I was working on a simple java based desktop application today. It was packaged in an executable jar. One of its required features was to launch another executable jar in a separate instance of JVM. Here is my code dealing with this requirement:
1 2 3 4 5 6 |
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "TestApp2.jar"); try { pb.start(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Couldn't start the second app.\n" + ex.toString()); } |
This is equivalent of running following command on terminal.
1 |
java -jar TestApp2.jar |
This code was working fine on Windows and Ubuntu. But on Mac, it resulted in a dialogue like this:

It was a surprise for me as the first app was running fine on the same machine under same configuration. I knew for sure that I have installed Oracle JRE 1.8u60 on this system so java executable should be available on the command line. To investigate further, I launched the Terminal and run which java. It returned /usr/bin/java. Then I run java -version which returned:
1 |
No Java runtime present, requesting install. |
and the same dialogue appeared again.
I had no idea how to proceed further. So I started searching on Google. It turned out that, by default, /usr/bin/java is a symlink to /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java . To make it work on command line, we have to delete it and then recreate it pointing it to the java command in newly installed Oracle JRE which resides at /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin.
Thus, the two commands that we need to run to fix this issue are given below:
1 2 |
sudo rm /usr/bin/java sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin |
An alternative solution suggested by some other people is to install the full JDK instead of just the JRE. But I have not tested this option because I don’t think it would be appropriate for me to ask a user to install full JDK just to run my app. JRE should be enough for this purpose.
References:
I also add the following to
~/.bashrc
:export JAVA_HOME=$(/usr/libexec/java_home)
*******-iMac:Downloads ********$ sudo rm /usr/bin/java
rm: /usr/bin/java: Operation not permitted
*******-iMac:Downloads ********$ sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin
ln: /usr/bin/java: Operation not permitted
– I keep getting this, it wont even let me put in my password, can you help me please?
Is it because of System Integrity Protection? I just installed the JDK instead and that seemed to sort it for me.
Better option: create the link under /usr/local/bin that is not integrity protected. https://stackoverflow.com/a/38435256/1097104