1

I'm trying to run a jar file "reportTool.jar" from another java application using the lang.Process class.

        ...
        String os = System.getProperty("os.name").toLowerCase();
        String shell = "";

        if (os.contains("win")) shell = "cmd";
        else shell = "/bin/sh";

        logger.info(shell);

        Process process = Runtime.getRuntime().exec(shell + " java -jar " + reportToolJar);

        logger.info("Waiting for reportTool");

        InputStream in = process.getInputStream();
        InputStream err = process.getErrorStream();

        BufferedReader brIN = new BufferedReader(new InputStreamReader(in), 999999);
        BufferedReader brERR = new BufferedReader(new InputStreamReader(err), 999999);

        String lineERR, lineIN;

        while ((lineERR =  brERR.readLine()) != null) {
            logger.error(lineERR);
        }
        brERR.close();

        while ((lineIN = brIN.readLine()) != null) {
            logger.info(lineIN);
        }
        brIN.close();
        process.waitFor();
        ...

However, when I go to execute I get

411  [main] ERROR reportToolController.ReportToolController  - /usr/bin/java: /usr/bin/java: cannot execute binary file

To note, reportToolJar is a String holding the absolute path to the jar file, this path is correct.

reportToolJar runs fine when being run from Terminal using the very same command that is executed in the code above.

5
  • Make sure the user you're using to execute your app has enough privileges to execute this shell script and the file has execution attribute. Commented Jan 6, 2015 at 15:38
  • Offtopic, but your code should be converted to use try-with-resources rather than manually handling the resources. Commented Jan 6, 2015 at 15:41
  • @LuiggiMendoza - "-rwxr--rwx 1 samuelsmith admin 13141512 3 Jan 23:52 reportTool.jar", all users can read, write, execute the jar. Commented Jan 6, 2015 at 15:50
  • (a) are you running this from the same user account that you use command line from? (b) You are probably using a different shell. From command line, can you do echo $SHELL? Then can you try the same java code with that shell instead of /bin/sh? Commented Jan 6, 2015 at 15:50
  • @RealSkeptic (a), yes; (b) echo $SHELL yielded "/bin/bash", changing this in the code left the same error. Commented Jan 6, 2015 at 15:53

1 Answer 1

3

This is what happens:

You are actually executing the command

/bin/sh java -jar <jar>

Not the command

java -jar <jar>

If you try the first one from command line, you'll get the same error you got from your process.

The reason for this is that bash (which I'm guessing /bin/sh is actually pointing to), when given a file on its command line, actually only accepts a text file, which it expects to contain shell commands.

So, if you had a text file such as "test.txt" which contains

java -jar <jar>

Even if that file is not set with executable permissions and doesn't have the #!/bin/bash magic line, it would be executed properly.

But a binary, executable file (like /usr/bin/java) is not an acceptable shell script as far as bash is concerned, and you get the error above.

Possible solutions:

  • Drop the shell and execute java directly.
  • Create a small shell script that calls your command and execute it.
  • Use /bin/bash -c "java -jar <jar>". Note that the whole command including its parameters needs to be passed as a single argument, hence the quotes.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, just went with the first solution.

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.