3

I have a java program which throws some exception,I tried executing it from shell script and printing 0 on failure and 1 on successful execution of java program.But It also printing the Exception onto console I just want to print exit code only.How to do this ?.Any suggestion are appreciated .

following are my Java program and script files Test.Java

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

Test.sh(script file)

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

getting the following out put

$sh Test.sh
    Exception in thread "main" java.lang.ArithmeticException: / by zero
        at EchoTest.main(EchoTest.java:3)
    0
$

Expecting output is like below(i.e just want to skip the exception message)

$sh Test.sh 0 $

1
  • 1
    a word of warning: every other system would expect a 0 for success and any other value for a series of error codes Commented Jul 29, 2015 at 6:07

3 Answers 3

2

Try this.

java Test 2> /dev/null
if [ $? -eq 0 ]
then echo "1"
else echo "0"
fi
Sign up to request clarification or add additional context in comments.

5 Comments

Hi @t_thirupathi, thank for your response ,it is just returning 0(zero) in both success and failure cases
that's because $? holds the success code of the redirection (>) rather than the java sentence
@prasad That means there is some other problem with running java Test, so it is exiting with non-zero always (in which case the script outputs 0).
@malarres No, redirection does not matter. $? has the exit code of the last command and redirection is not a command.
then my other answer is more complex than needed. will remove right now
1

you have to catch the exceptions. After that, you would be able to output exactly what you want. on your example:

public class EchoTest {
public static void main (String args[]) {
    try{
        System.out.println ("scuccess Prasad Bezavada "+(2/0));
    } catch (Exception e){
        // doing nothing is ok for your intended behaviour
    }
}
} 

1 Comment

you might also want to consider catching even Throwable instead of Exception, as it includes also Errors (including for example OutOfMemoryError).
0

First of all, you would like your Java program to return a value (either 1 or 0). In our case we will consider that if an exception is thrown, 1 will be returned and 0 otherwise. Also, exception will be hid (which is a bad practice. You should always log exceptions at least if you are not willing to show it on screen)

public class EchoTest {
    public static void main (String args[]) {
        try {
            System.out.println ("scuccess Prasad Bezavada "+(2/0));
            System.exit(0);
        }
        catch (Exception e) {
            // log your exception here
            System.exit(1);
        }
    }
} 

Once this is done then what you will need to work on is on getting java's output code.

java Test
output = $?
# do some logic here..
if [[ $output -eq 0 ]]; then
    echo "executed"
else
    echo "exception thrown"
fi

Finally, this will indeed return either 1 or 0 depending on execution ignoring exception case, which is what you actually requested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.