How to return some value to a shell from a Java program?

I want to run a Java program from a shell script. So I did something like below

My Java test file is as follows

public class EchoTest { public static void main (String args[]) { System.out.println ("scuccess ..!!"); } 

My test script file is as follows

 out=$(java EchoTest) echo $out 

I compiled a java program and then I ran this shell script (e.g. $ sh Myscript.sh). Now he printed the output on the console. Now it works fine.

If I write a program as shown below (which causes some kind of exception)

  public class EchoTest { public static void main (String args[]) { System.out.println ("Value is "+(2/0)); } 

it just prints java exception on console. But my requirement is that I want it to print 0 or 1 on the console, i.e. I want to get 0 (zero) when my java program fails and you want to get 1 (one) when the java program succeeds.

+7
java linux shell
source share
4 answers

The documentation from the java program says:

EXIT STATE

The following output values ​​are usually returned at startup, usually when the caller is called with incorrect arguments, serious errors, or exceptions thrown from the Java virtual machine. However, a Java application can select any value using the System.exit (exitValue) API call.

  • 0: successful completion.
  • > 0: An error has occurred.

So, if you do nothing, the JVM follows the general convention of returning a 0-value to the caller on success and a non-zero value in case of an error.

Your shell script should be as follows:

 java EchoTest if [ $? -eq 0] then echo 1 else echo 0 fi 

This means that you can ignore standard output and standard error and simply rely on the exit status of the JVM.

As suggested by @alk, you can even replace the first line out = $( java EchoTest ) and use $out in the success branch (when $? Is 0 )

+5
source share

you need to use System.exit(code) with the right code if you find an error / exception or not

you will have

 System.exit(0) // if you detect error, you need to handle exception System.exit(1) // when no error 
+4
source share

A more complete example of processing a program from a shell might be

 #!/bin/bash # # run.sh command # # Runs command and logs the outcome to log.<pid> and err.<pid>. # cmd=${1} # Run cmd and log standard output (1) to log.<pid> and standard error (2) to err.<pid>. $cmd 1>log.$$ 2>err.$$ # Copy the return code from cmd into result. result=$? # Test whether result is 0. if [ $result -eq 0 ]; then echo "$cmd" succeeded. cat log.$$ else echo "$cmd" failed with result = $result! cat err.$$ fi exit $result # eof 

$$ evaluates the pid shell, so the code can be run in parallel with the copies themselves.

Call the script above, for example

 $ run.sh "java myprogram" 
+1
source share

Thank you so much for your answers. I got a solution with the following script, but I don’t know what is going on here, Just explain to me what is going on here.

 cmd="java EchoTest" $cmd >log.$$ 2>err.$$ result=$? if [ $result -eq 0 ]; then echo "1" else echo "0" #echo $result fi rm log.$$ >/dev/null rm err.$$ >/dev/null 
0
source share

All Articles