MacOS – Managing multiple Java Installations

javamacosterminal

After hours of Googling I'm a little depressed with the results..

For work, I have multiple Java Installations, some of them via SdkMan, and some of them seem to found there way into the /Library/Java …

Now to the problem: If I try to start any .jar File (self coded or not) via double-click, it just says 'Couldn't open jar file, check console for errors'.

But if I try to start the same jar File from Terminal, it works just fine.

I know for sure that my Terminal uses another Java Installation than the MacOS 'JarLauncher.app', but I can't find a way to specify the Java installation for this. Under System Settings > Java, there should open a Java Control Panel, but it refuses to open, even after hitting the re-open button several times.

I tried removing the old Java Installation files with the guides I found online, but so far no luck.

Does anyone know how to solve those problems? It's my daily work to write little .jar file Scripts to help colleagues on their workflows.

Currently installed are: Java8u121 via SDKman and /Library/Java Folder, and Java9 via normal Oracle Install Tried removing the others, but not sure if I cleaned them up good enough.

Sadly it's no option to reset the whole system, it's a company managed laptop, and I don't know what will happen to all connected Systems, most of all the ActiveDirectory Sync.

Using MacOS Sierra 10.12.6, not allowed to upgrade to High Sierra yet.

Best Answer

The /Library/Internet Plug-Ins/JavaAppletPlugin.plugin (the JRE) is the one that launches the executable jar though 'JarLauncher.app' - and it is the one installed from Oracle - and it is the last installed JDK.

So if you say you have JDK 9 from Oracle, as the only one from Oracle, then it will be JavaAppletPlugin.plugin of JDK 9 that should be used right now - which doesn't work together with JDK 8 (out of the box).

One thing you could try is to install all your JDKs from Oracle, and then one by one save the JavaAppletPlugin.plugin of each version you want to use in a folder of choice, from where you then copy/override the current plugin in /Library/Internet Plug-Ins/ - maybe create a small Terminal script, containing setting JAVA_HOME and copy/override of plugin - you will then execute this script just before you want to test your executable jar.

EDIT:

I think after some thinking, that you first need to do some clean up.

This means delete /Library/Internet Plug-Ins/JavaAppletPlugin.plugin and all JDKs under /Library/Java/JavaVirtualMachines/.

Then download all needed JDKs from Oracle, and install them one by one - lowest version first, and remember to backup the installed /Library/Internet Plug-Ins/JavaAppletPlugin.plugin after each install to a folder of choice.

I have saved my plugins with the names:

  • JavaAppletPlugin-7.0.80.plugin
  • JavaAppletPlugin-8.0.162.plugin
  • JavaAppletPlugin-9.0.4.plugin

You should also be able to open the java control panel for each installed JDK - but only for the one you currently has installed.

Now you can create a script which updates the /Library/Internet Plug-Ins/JavaAppletPlugin.plugin - below is a quick script I did where it install the JRE 7_80 plugin (replace <PATH_TO_BACKUP_OF_PLUGINS> with you path) - which also is the java control panel, so you should be able to open it from Settings.

#!/bin/sh

sudo rm -rf "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin"
sudo cp -rf "<PATH_TO_BACKUP_OF_PLUGINS>/JavaAppletPlugin-7.0.80.plugin" "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin"

If you want one script for each plugin, or you'll create one script which takes a version input, is up to you.

JavaAppletPlugin.plugin and JAVA_HOME need to follow each other, maybe you already has a way for switching JAVA_HOME today.

The way I usually set my JAVA_HOME is like below, where I have added some functions to my .bash_profile

jdk7() {
   export JAVA_HOME=`/usr/libexec/java_home -v '1.7.*'`
   export PATH=$JAVA_HOME/bin:$PATH
}

jdk8() {
   export JAVA_HOME=`/usr/libexec/java_home -v '1.8.*'`
   export PATH=$JAVA_HOME/bin:$PATH
}

jdk9() {
   export JAVA_HOME=`/usr/libexec/java_home -v '9.*'`
   export PATH=$JAVA_HOME/bin:$PATH
}
Related Question