LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2011, 07:46 AM   #1
ashish.ars@gmail.com
LQ Newbie
 
Registered: Oct 2008
Posts: 6

Rep: Reputation: 0
Only part of shell script executed from Java program.


I am facing some problem in running shell script from a java program. The script runs fine when executed from the terminal. But, when executed from the java program, not whole script is executed. My java code to implement shell script execution is:

File file = new File("/path/to/script");
String COMMAND= "./run";
ProcessBuilder p = new ProcessBuilder(COMMAND);
p.directory(file);

try {
Process start= p.start();

} catch (IOException e) {

e.printStackTrace();
}

It seems only the first line is executed. "run" is the script name. I am using Ubuntu. The script is:

#start of script
java net.tinyos.sf.SerialForwarder -comm serial@/dev/ttyUSB0:telosb
if cygpath -w / >/dev/null 2>/dev/null; then CLASSPATH="oscilloscope.jar;$CLASSPATH" else CLASSPATH="oscilloscope.jar:$CLASSPATH" fi
java Oscilloscope
#end of script

Any suggestions why the whole script is not being executed? I observed that only the 1st command was executed.

Thanks.
 
Old 11-01-2011, 01:11 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Presumably you are seeing evidence that java net.tinyos.sf.SerialForwarder starts. Do have have any evidence that it finishes?

Presumably you are not seeing any evidence that java Oscilloscope starts.

You could "instrument" the script to see what it does do
Code:
#start of script
tmp_file=/tmp/a-safe-name-nobody-else-would-use
> $tmp_file
java net.tinyos.sf.SerialForwarder -comm serial@/dev/ttyUSB0:telosb
echo 'DEBUG: java net.tinyos.sf.SerialForwarder returned' >> $tmp_file 
if cygpath -w / >/dev/null 2>/dev/null; then 
    echo 'DEBUG: test passed' >> $tmp_file 
    CLASSPATH="oscilloscope.jar;$CLASSPATH" 
else 
    echo 'DEBUG: test failed' >> $tmp_file 
    CLASSPATH="oscilloscope.jar:$CLASSPATH" 
fi
echo 'DEBUG: calling java Oscilloscope' >> $tmp_file 
java Oscilloscope
echo 'DEBUG: java Oscilloscope returned; script exiting' >> $tmp_file 
#end of script
 
1 members found this post helpful.
Old 11-01-2011, 02:01 PM   #3
ashish.ars@gmail.com
LQ Newbie
 
Registered: Oct 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hi,

Thanks for the reply. Adding the debug lines on the script had the following output in the tmp/DebugFile:

DEBUG: java net.tinyos.sf.SerialForwarder returned
DEBUG: test failed
DEBUG: calling java Oscilloscope
DEBUG: java Oscilloscope returned; script exiting

It shows that the end of the script is reached. But, why doesn't java Oscilloscope start? Is it because of the Classpath settings inside the script?

Ashish.
 
Old 11-01-2011, 09:18 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,166

Rep: Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680Reputation: 2680
Try
Code:
which java >>$tmp_file
I suspect you have no (or minimal) env settings in the new cmd shell ...

Actually, try this at the top
Code:
set -xv
 
1 members found this post helpful.
Old 11-02-2011, 03:08 AM   #5
ashish.ars@gmail.com
LQ Newbie
 
Registered: Oct 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hi,

which java >>$tmp_file: gave the following output:
/usr/bin/java

Does it say that the execution is taking place from that directory? But, what about p.directory(file)? It should set the directory to the new one specified in the java code. I couldn't find where the problem is.

Thanks.
 
  


Reply

Tags
java, shell script, ubuntu


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
Running shell script in Ubuntu from a java program ashish.ars@gmail.com Ubuntu 4 10-28-2011 05:47 AM
How can I call a C program in a Shell Script from Java habibur Programming 3 10-24-2009 02:09 AM
booting to a fullscreen java program with a shell script sebastian.raghe Linux - Newbie 3 05-19-2005 02:42 AM
Creating a shell script to run Java program paultaylor Programming 7 11-12-2004 03:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:02 PM.

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