LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2004, 09:52 AM   #1
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Rep: Reputation: 15
Angry Creating a shell script to run Java program


Hi Ive just moved over to Linux from Windows and I am trying to convert a Windows batch file to a unix shell script to run a java application, but it so long since Ive used UNIX I cant really remember how to do it.

My script contains


#!/bin/sh
java -jar lib/testapp.jar



These are my failed attempts to run it, not too sure what the differences are but I think the 3rd one is the only one to actually find my shell script.


[root@]# testapp.sh
-bash: testapp.sh: command not found
[root@]# ./ testapp.sh
: bad interpreter: No such file or directory
[root@]# . testapp.sh
Unable to access jarfile lib/testapp.jar



if I just do this at the command line it works fine


[root@]#java -jar lib/testapp.jar
 
Old 11-11-2004, 10:49 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
I usually use a little work around for java like this:

1 - Create a little file
touch littleapp

2 - Insert something into the app to be executable
echo "java -jar lib/testapp.jar" >> littleapp

3 - Make it executable
chmod +x littleapp

4 - Move it to /usr/bin or /sbin to be able to run it from a shell
mv littleapp /usr/bin

I know this is not the way you wanna do it, but it's another way of doing it that I wanted to let you know. I so need to learn shell scripts .

Good luck!
 
Old 11-12-2004, 05:11 AM   #3
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Original Poster
Rep: Reputation: 15
No its still not working. ive just simplified it to run java -version, but still it wont work in shell script

[root@cpc1]# cat testapp
/opt/jdk1.5.0/bin/java -version
[root@cpc1]# testapp
Unrecognized option: -version
Could not create the Java virtual machine.
[root@cpc1]# /opt/jdk1.5.0/bin/java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)
 
Old 11-12-2004, 06:31 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Your editor is probably adding garbage (i.e. CR) at end of lines.
What gives "od -c testapp" ?
 
Old 11-12-2004, 07:12 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
do you actually *have* /bin/sh installed?

try changing it to:
Code:
#!/bin/bash
remember it must be right at the first line first column.


billy
 
Old 11-12-2004, 08:24 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
do you actually *have* /bin/sh installed?
That wouldn't explain the "Unrecognized option: -version" problem.
 
Old 11-12-2004, 09:11 AM   #7
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Original Poster
Rep: Reputation: 15
Thankyou, od -c has cracked the problem i have \r at the end.
I recreated the file from scratch and it is now runs ok (im suprised it causes such a problem though)

As for bin/sh the file does exist but is actually a symbolic link to /bin/bash the script seems to run ok without the line at all. So is there any reason to have this line
 
Old 11-12-2004, 03:11 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You shouldn't be surprised, the end of line character (\n) under unix is the single line-feed character, and not the double carriage-return/line-feed like DOS/Windows.
Moreover, CR (\r) is a valid filename character for most unix supported file-systems, so it isn't forbidden, although not a smart idea, to have one interpreter named /bin/sh and another one named /bin/sh\r , and the #! construction should be able to distinguish between them.

About the bash question, it is probably a good idea to leave /bin/sh instead of /bin/bash, as it makes your script more portable, unless you use bash specifics unsupported on the real bourne shell.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a script run through a shell program... okeyla Linux - Newbie 1 09-15-2005 10:06 AM
booting to a fullscreen java program with a shell script sebastian.raghe Linux - Newbie 3 05-19-2005 02:42 AM
unix korn shell script that interact with windows java program ann124 Programming 5 11-09-2004 09:14 PM
how to run a shell script type program redragon7964 Linux - Newbie 1 04-06-2004 02:03 AM
run shell script in c program u4u Linux - General 1 02-14-2004 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration