MacOS – How to use Jar Launcher with specified jdk version

javamacos

Now I have multiple jdk installed on OSX as shown below:

└─[1] <> /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    14.0.1, x86_64: "Java SE 14.0.1"    /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home
    1.8.0_241, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home

Then I want to double click to run a jar file directly, for instance this:

jar file

However, it warns me that this jar file is launched with JDK 14 but I want to launch it with JDK 1.8.

A solution is that run it in terminal with "java -jar" in specified jdk path,but clearly it is not elegant.

The best way is to let "Jar Launcher" use specified jdk version ,but I don't know how.

enter image description here

Best Answer

After long time efforts,I finally find the solution,that is ,to make your own Jar Launcher app:

  1. Launch Automator to make Automator Quick Action

    enter image description here

  2. Select Application
  3. Look for the Run Shell Script action with Filter Finder Items and add it to the right.
  4. paste scripts here like this below:

    enter image description here I make some changes to make it better

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
cd "$(dirname "$@")" 
java -jar "$@" 
  1. save this quick action as an app for example "open jar.app"

  2. go to /System/Library/CoreServices/Jar Launcher.app/Contents/Resources and copy JarLauncher.icns enter image description here

  3. cd to "open jar.app" you just made and paste JarLauncher.icns to "open jar.app/Contents/Resources"
  4. edit "open jar.app/Contents/Info.plist" and change key(AutomatorApplet to JarLauncher.icns):
    <key>CFBundleIconFile</key>
    <string>JarLauncher.icns</string>

enter image description here

  1. now you can choose to open with "open jar.app" just like before. enter image description here