LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Only part of shell script executed from Java program. (https://www.linuxquestions.org/questions/linux-newbie-8/only-part-of-shell-script-executed-from-java-program-911242/)

ashish.ars@gmail.com 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);
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.

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


ashish.ars@gmail.com 11-01-2011 02:01 PM

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.

chrism01 11-01-2011 09:18 PM

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

ashish.ars@gmail.com 11-02-2011 03:08 AM

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.


All times are GMT -5. The time now is 01:43 PM.