Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable JAR on Ubuntu (NetBeans)

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!

like image 1000
Sarah Avatar asked Aug 04 '11 22:08

Sarah


People also ask

How do I make a .jar executable in Ubuntu?

However, to make the jar file itself executable, you need to set the executable bit, as the message hints. chmod +x /path/to/your/file/myFile. jar will accomplish this. After that you can do ./myFile.

How do I run an executable jar file in Linux?

Right click the . jar file, and open properties. Now in the properties menu, open the 'Permissions' tab. Enable the 'Allow executing this file as program' checkbox.

How do I make a Java JAR file executable?

Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.


1 Answers

  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.

like image 87
Paul Avatar answered Sep 23 '22 13:09

Paul