I want to run a Java program from a shell script.So I did something like below
My test Java file is as follows
public class EchoTest {
public static void main (String args[]) {
System.out.println ("scuccess ..!!");
}
My test shell script file is as follows
out=$(java EchoTest)
echo $out
I have compiled the java program, and then I did run that shell script (like $sh Myscript.sh). Now it printed the output onto console.Upto now it is working fine.
If I write a program like below (which throws some exception)
public class EchoTest {
public static void main (String args[]) {
System.out.println ("Value is "+(2/0));
}
it is just printing the java exception onto console. But my requirement is that I want it to print 0 or 1 onto console, i.e I want to get 0(zero) when my java program fails and want to get 1(one) when java program executes successfully .
echo $err?2/0is always going to throw anArithemeticExceptionbecause math doesn't allow you to divide by zero.