11

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?

2
  • It probably opens a terminal window, finishes and then closes the terminal window. Commented Sep 24, 2012 at 11:33
  • To debug in Terminal.app - goto Preferences->Settings choose the Shell tab and Alter the "When the Shell exits" option then the terminal will stay open
    – mmmmmm
    Commented Sep 24, 2012 at 12:20

4 Answers 4

8

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...).

1
  • 1
    I've used Macs since 1984, but particularly moreso since 2001 and MacOS X. I've used Java since before it was released. And I was today years old when I learned you could do this!
    – Vihung
    Commented Sep 2, 2021 at 12:08
2

The best way to make a java gui application work with OSX is to bundle it as an application. This requires Jar Bundler.app which on Lion is at /usr/share/java/Tools/Jar Bundler.app. I think it is installed as part of Xcode but appears not to be in Apple's current documentation so might not now be supported. Old documentation is here

There do appear to be ant tasks to do this in appbundler which is documented in the Oracle JavaSE documentation.

2
  • Actually its a game. So how do I make people double-click on it and open up my jar without commands? Commented Sep 24, 2012 at 12:25
  • Other users will prefer it as an OSX app rather than just a .jar. In this case you do it once as part of the build process and users do not have to do anything
    – mmmmmm
    Commented Sep 24, 2012 at 12:27
2

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
0

packr

You can use packr to package up your jar for easy distribution and double-clicking to open:

Packages your JAR, assets and a JVM for distribution on Windows (ZIP), Linux (ZIP) and Mac OS X (.app), adding a native executable file to make it appear like the app is a native app. Packr is most suitable for GUI applications, such as games made with libGDX

packr has been discussed on Hacker News and is an open source project.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .