-   Linux - Newbie (
-   -   Only part of shell script executed from Java program. ( 11-01-2011 07:46 AM

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);;

try {
Process start= p.start();

} catch (IOException e) {


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.


catkin 11-01-2011 01:11 PM

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

#start of script
> $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
    echo 'DEBUG: test failed' >> $tmp_file
echo 'DEBUG: calling java Oscilloscope' >> $tmp_file
java Oscilloscope
echo 'DEBUG: java Oscilloscope returned; script exiting' >> $tmp_file
#end of script 11-01-2011 02:01 PM


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?


chrism01 11-01-2011 09:18 PM


which java >>$tmp_file
I suspect you have no (or minimal) env settings in the new cmd shell ...

Actually, try this at the top

set -xv 11-02-2011 03:08 AM


which java >>$tmp_file: gave the following output:

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


All times are GMT -5. The time now is 09:19 PM.