Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting exit value of a shell script 255 every time

Tags:

java

shell

ksh

I have a jar say test.jar having TestJar as its main class, a shell script jar_executor.sh,and a java file. My test.jar will return 1 if we pass 1 as an argument and 2 if we pass any other value. My shell script is executing test.jar as follow

#!/usr/bin/ksh
java TestJar 1 -cp test.jar
return_code=$?
exit $return_code 

In java file I am creating a process and executing shell script and taking its exitvalue with following code

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(sh jar_executor.sh);
int exitVal = process.waitFor();
System.out.println(exitVal);

This exitVal variable should print 1 or 2 as per argument we are passing but it is printing 255 everytime. If I use echo $return_code in shell script then I am getting correct value. Please help me why I am getting value 255 with exit. Thanks in advance!!!

like image 998
shoaib Avatar asked Nov 05 '22 19:11

shoaib


1 Answers

255 or -1 is an application defined exit code, you would have to read the code to know what it means.

A Java application which exits normally, returns 0 and if it throws an Exception, returns 1.

like image 93
Peter Lawrey Avatar answered Nov 12 '22 11:11

Peter Lawrey