Getting script 255 shell exit value each time

I have a jar say test.jar, in which TestJar is its main class, the shell script jar_executor.sh and the java file. My test.jar will return 1 if we pass 1 as an argument and 2 if we pass any other value. My shell script executes test.jar as follows

#!/usr/bin/ksh
java TestJar 1 -cp test.jar
return_code=$?
exit $return_code 

In a java file, I create a process and run a shell script and taking its exitvalue value with the following code

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(sh jar_executor.sh);
int exitVal = process.waitFor();
System.out.println(exitVal);

This variable exitVal should print 1 or 2 according to the argument we pass, but it prints 255 each time. If I use echo $return_codea script in the shell, then I get the correct value. Please help me why I get the value 255 with exit. Thanks in advance.

+5
5

255 -1 - , , , , .

Java-, , 0 , , 1.

+2

, workspace.metadata.plugins\org.eclipse.e4.workbench workbench:). eclipse

+2

, Java. , , ; . Java. Java , , . JRE , , , , concurrency.

, JRE , . , sleep 1 script .

0

script java :

java TestJar 1 -cp test.jar

java, . Java- . java . script: : TestJar. , jar_executor.sh script:

java -cp test.jar TestJar 1

255 , , : java 255 1, korn (/usr/bin/ksh) , script .

, :

jar_executor.sh

#!/bin/sh -e

java -cp test.jar TestJar 2
return_code=$?
exit $return_code

TestJar.java

public class TestJar {
    public static void main(final String[] args) {
        System.exit(Integer.parseInt(args[0]));
    }
}

JarRunner.java

import java.io.IOException;

public class JarRunner {
    public static void main(final String[] args) throws IOException, InterruptedException {
        final Runtime runtime = Runtime.getRuntime();
        final Process process = runtime.exec("sh jar_executor.sh");
        final int exitVal = process.waitFor();
        System.out.println(exitVal);
    }
}

java -cp bin JarRunner, 2, .

0

, script. :

Process process = runtime.exec("sh -c jar_executor.sh");

"-c", , .

-1

All Articles