Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
It probably opens a terminal window, finishes and then closes the terminal window. –  The Unfun Cat Sep 24 '12 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 –  Mark Sep 24 '12 at 12:20
add comment

2 Answers

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

share|improve this answer
add comment

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.

share|improve this answer
    
Actually its a game. So how do I make people double-click on it and open up my jar without commands? –  SystemNetworks Sep 24 '12 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 –  Mark Sep 24 '12 at 12:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.