When i try to run any Runtime.exec() command like this simple one to print the java version:
String [] cmd = { "java", "-version" };
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
BufferedReader stdError = new BufferedReader( new InputStreamReader( process.getErrorStream() ) );
String s = null;
while ( ( s = stdError.readLine() ) != null )
System.out.println(s);
It works just fine, but if i am using a JRE bundled with my Java application i always get errors saying that java is unknown:
java.io.IOException: Cannot run program "java": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at org.runner.Runner.main(Runner.java:11)
Why does this happen when using a bundled JRE?
I ran into this problem on MacOS too. It only affected my application when using my bundled jre. While switching to 'fork' (as you did in your answer) worked for me. I was curious that there must be a 'better' solution since the bug report you pointed to was filed and fixed years ago. So after a little more digging, I found that, on MacOS there is a executable in the bundled jre (../jre/Contents/Home/jre/lib/jspawnhelper) that needs to be executable in order for Runtime.exec() to work. It was not, but as soon as I made it executable my processes ran without error.
To make it executable, I added this to the build.properties file in feature that bundles the jre:
root.macosx.cocoa.x86_64.permissions.755=jre/Contents/Home/jre/lib/*
You may need to make adjustments to that line depending on you arch and jre location.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With