Executable JAR on Ubuntu (NetBeans)

Sarah picture Sarah · Aug 5, 2011 · Viewed 18k times · Source

I'm writing a simple Swing application in NetBeans and doing so on an Ubuntu machine for the first time.

As many of you know, NetBeans automatically creates executable JARs for projects that are "set as main".

On Windows, you can double-click an executable JAR and it automatically invokes the JRE and runs the app. In Ubuntu, double-clicking the .jar file causes the file to be opened in the archive manager instead. In order to run my JAR, I either have to right-click it and select "Open with OpenJDK Java 6 Runtime" or launch it from the command line.

From the command line I get no problems whatsoever. However, when I try launching it from the right-click menu, I get an error that reads:

The file MySwingApp.jar is not marked as executable...

So I have 2 questions:

  1. What do I have to do to set it as executable? Is this something I can do inside NB or do I have to use the shell? If I have to set permissions via the shell, doesn't that conflict with NB's policy of auto-generating **executable** JARS? And what command would I use to flip the executable bit anyhow?!?!
  2. Is this just a Linux hiccup? I want to send this JAR to friend who run Windows and I'd like for them to be able to just double-click it and have the program launch

Thanks for any helpful suggestions!

Answer

Paul picture Paul · Aug 5, 2011
  1. You will need to manually tweak your build process to get the jar file marked as executable in Netbeans. Go to your project root and open build.xml. The header has instructions on adding to the build process. There is a target, "-post-jar", that is called after the jar is built. You'll need to make that target and use Ant's chmod task to modify your jar. Once you do that it will occur every time you make a jar file in that project.

  2. It will run fine on your friend's Windows machine, as long as he has a JRE installed.

Here is a thread about running jars using double click in Linux.