8

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 .

7
  • error will not go to normal output, did you try echo $err? Commented Jul 28, 2015 at 15:19
  • Hi @FredericHenri, thank you for your prompt response ,actually I dont want to print any exception of java program onto console Just want return zero on success and 1 on failure.Please help me to do this Commented Jul 28, 2015 at 15:20
  • 1
    If you're looking to return a unix-style return value, look at System.exit(int). Commented Jul 28, 2015 at 15:23
  • Not like that @chsh,simply I want to print zero or some my own failure message when java program fails Commented Jul 28, 2015 at 15:25
  • 1
    2/0 is always going to throw an ArithemeticException because math doesn't allow you to divide by zero. Commented Jul 28, 2015 at 15:27

4 Answers 4

6

The documentation from the java program says :

EXIT STATUS

The following exit values are generally returned by the launcher, typically when the launcher is called with the wrong arguments, serious errors, or exceptions thrown from the Java Virtual Machine. However, a Java application may choose to return any value using the API call System.exit(exitValue).

  • 0: Successful completion.
  • >0: An error occurred.

So if you do not do anything, the JVM follows the common convention of returning a 0 value to the caller on successfull completion and a non null value in case of error.

Your shell script should then be :

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

That means that you can ignore standard output and standard error and just rely on the exit status from the JVM.

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

Sign up to request clarification or add additional context in comments.

6 Comments

This could also be combinded with the OP 1st approach by doing out=$(java EchoTest) and printing out out only in the "success"-branch, will say if $? equals 0.
@prasad: Send the error message to an error file by rediredcting stdout to an error file like thisout=$(java EchoTest 2>error.log) and cat error.log in the "failure" branch or if you are not interested in the error message just replace error.log by /dev/null.
Hi @alk ,you mean like this out=$(java EchoTest 2>error.log) if [ $? -eq 0 ] then echo "1" else cat error.log fi
@prasad : there's a slight difference when passing by a file : you should avoid starting 2 or more shells at the same moment. But provided you only start one, it is certainly a nice and simple solution. An alternative not using a file would require a dedicated program.
@SergeBallesta can you please provide the script?
|
4

you need to use System.exit(code) with the code you want depending if you detect 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

2 Comments

I'm also going to note here that most programs return 0 when the program runs successfully and a number higher than 0 to represent specific errors.
clear but he specifically mentioned 0(zero) when my java program fails and want to get 1(one) when java program executes successfully.
1

A more complete example on handling a program's out from the 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 shell's pid, so the code can be ran in parallel with copies of itself.

Call the script above like this

$ run.sh "java myprogram"

3 Comments

Hi @alk , finally I got expected output like from this script ,
Hi@alk ,can you explain this line of statement ,cmd="java EchoTest" $cmd >log.$$ 2>err.$$ result=$?
@prasad: To understand what is going on in this line you want to read on Standard Streams. You can do this for example here: en.wikipedia.org/wiki/Standard_streams
0

Thank you so much all ,for your replies. I got the solution with following script ,but I don't Know whats happening here, Just please explain 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

2 Comments

@prasad : this is not really an answer, but I understand you could not put the code in a comment :-) . If the comments that alk added to its answer are enough, you should delete it (and accept alk's answer ...)
Yeah @SergeBallesta ,this solution is based on alk's comment only,I just modified it a little bit

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.