MacOS – How to run a jar when I double-click it

javamacos

So, I double click on my jar file and it just blinks a window with white screen then it disappears. After that, I right-click and select "Open with jar launcher" it is the same. too. I opened up Terminal and type java -jar "System FRONT.jar" then it opened up perfectly.

I want it to be opened directly when double clicking it! How do I do that?

Best Answer

You can prepend a short script to the .jar to make it executable via double click. In Terminal do the following:

$ cat > header-template <<"EOF"
#!/bin/sh

exec java -jar $0 "$@" > /dev/null

EOF
$ cat header-template "System FRONT.jar" > executable_app
$ chmod +x executable_app

Double-clicking executable_app should now launch the application.


Why does this work in the first place? Well, a .jar is just a .zip archive which gets unpacked and executed by java. And the zip format allows to prepend additional stuff in front of the actual archive. Any zip unarchiver skips this part until it finds the start of the archive (indicated by PK...).