Get Return Code of a Program within a Java Program

In previous post i wrote about shell scripts arguments having spaces. Now, in this post i am writing about how to get return code of a program within a java program.

Problem statement:

Get return code of a program while you execute that program from the java code. This program may be either a shell command/script or another java program.

Get Java Return Code POC:

The following java code snippets are written for this POC.

Program 1:

I have a simple java application which return the same integer as its return code which i pass it as an argument. Following i have shared that.

package com.harisaltaf;

public class ReturnCode {
public static void main(String[] args) {
System.out.println("Returning code=" + args[0]);
System.exit(Integer.parseInt(args[0]));
}
}

Program 2:

I have written another java application which executes this application and get its return code. Following is that piece of code.

package com.harisaltaf;

public class GetReturnCode {
public static void main(String[] args) {
// String[] command = { "./linux_shell_script.sh" };
String[] command = {
"sh",
"-c",
"cd /home/haris/workspace/get_java_return_code"
+ "&& java -cp java_return_code.jar com.harisaltaf.ReturnCode 5" };
ShellExecutor executor = new ShellExecutor();
try {
executor.execute(command);

System.out.println("Command Return Code= "
+ executor.getReturnCode());
System.out.println("Command Result= " + executor.getScriptResult());
System.out.println("Command Error= " + executor.getScriptError());

} catch (Exception e) {
System.out.println(e.getMessage());
}
}

You can see i have written two commands. In first command i am executing the program 1 from a shell script while in second command i am running that directly as a shell command.

Below is the shell script code. Don’t forget to change shell script file writes.

echo ""
echo "---------------------------------------------"

echo "---result of ls:"
cd /home/haris/workspace
ls

echo "---result of java program:"
cd /home/haris/workspace/get_java_return_code
java -cp java_return_code.jar com.harisaltaf.ReturnCode 5

echo "---result of ls again:"
ls
echo "---------------------------------------------"

Output:

Executing Shell Script:

When i run the program with the help of shell script, i get the following output.

Command Return Code= 0
Command Result=
---------------------------------------------
---result of ls:
get_java_return_code
java_return_code
---result of java program:
Returning code=5
---result of ls again:
bin
java_return_code.jar
linux_shell_script.sh
src
---------------------------------------------

Command Error= null

You see in the Java code you didn’t get the correct return code of program 1 in you 2nd java application. The actual code of the program 1 should be equal to 5 as we gave it as input but the returned code of the shell script is 0 as in output “Command Return Code= 0”. This is because when you run some commands from shell script then the return code returned is the return code of the complete shell script not of only the program you specified.

When executing as direct Shell Command:

When i run the program with java command executing directly from java application, i get the following output.

Command Return Code= 5
Command Result= Returning code=5

Command Error= null

You see i get the correct return code now.

Source code:

Download source code from here: get_java_return_code