0

I have a Java program that uses Runtime.getRuntime.exec to run a Python script, for example:

System.out.println("Flying Circus - running monty.py on " + args[0]);
Runtime.getRuntime().exec("python monty.py " + args[0]);

It works properly on my personal computer, both running from class files and from a JAR file. However, when I move the JAR file to a different computer (with Java and the correct version of Python installed, although I have no idea if it's running official Java or an open-source knockoff such as Gnu Common Java), the python script invocation silently fails with no error.

What's more, when I take the exact same function calls and put them in a minimalistic program that only invokes the python script, it works properly.

I manually checked the program that is having problems and found no evidence of "gotchas" that would cause a silent failure including bad variable clearing, bad variable sharing, blocking/nonblocking and concurrency issues, or threading.

Can someone recommend an alternative or wrapper function that must run the given command and report any form of failures? Failures include: "program didn't run", "command not found on command line", and "program threw an exception or exited with a code other than 0".

user1258361
  • 1,133
  • 2
  • 16
  • 25
  • See https://stackoverflow.com/questions/8149828/read-the-output-from-java-exec and https://stackoverflow.com/questions/3643939/java-process-with-input-output-stream – Slaw Aug 09 '18 at 15:16
  • 1
    Also, `Runtime.exec` returns a [`Process`](https://docs.oracle.com/javase/10/docs/api/java/lang/Process.html) object which you can use to retrieve the exit code (as well as its in/out/err streams). – Slaw Aug 09 '18 at 15:19
  • Runtime.exec is obsolete. Do not use it. Use [ProcessBuilder](https://docs.oracle.com/javase/10/docs/api/java/lang/ProcessBuilder.html) instead, and call its [inheritIO()](https://docs.oracle.com/javase/10/docs/api/java/lang/ProcessBuilder.html#inheritIO()) method before starting the process, so all error output will be visible. – VGR Aug 09 '18 at 15:50
  • I refactored it to ProcessBuilder and it silently fails again. – user1258361 Aug 13 '18 at 19:08

1 Answers1

0

Figured out the problem. The other computer was using an open-source Java knockoff, probably Gnu Common Java or similar). I've previously had problems with Java programs failing for other stupid reasons on one of my old Linux VMs due to these Java knockoffs.

Installing official Oracle Java fixed the problem. Reminder: on Linux, you have to run the installer with sudo or it will silent-fail with no clue as to what happened.

user1258361
  • 1,133
  • 2
  • 16
  • 25