简体   繁体   中英

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!

  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.

You can Java like a native binary on Ubuntu (and other linuxes), it's a feature of the kernel. You need to install the binfmt-support package to give the kernel the hooks to run java in this way.

In Ubuntu open up a terminal and run:

sudo apt-get install binfmt-support

Then make your JAR file executable

chmod a+x yourjar.jar

Then you can run your JAR like any other binary by typing

yourjar.jar

我浏览了互联网,我看到了一篇文章,其中包含完整的步骤来运行jar文件http://mlartist.blogspot.in/2012/07/deployment-netbeans-project-in-linux.html

Jar-files aren't first class executables, and they don't become magically executables by changing their executable flag.

If you execute a jar, you run the command

 java -jar YOURJAR.jar ...

It's the same, as if you double click a png file, and expect it to run in a painting program

 gimp YOUR.png

You don't need to make your png an executable one, and it will not solve a problem.

Instead, you have to tell your desktop environment, what to do when double clicking a jar or png-File, and you have to do it on Linux the same way you do it in Windows - maybe the installer on Windows does it for you, because there is normally just on Desktop Environment (Windows) on the OS (Windows), but Linux has Gnome, KDE, XFCE, LXDE, fluxbox and millions more.

And it isn't so sure what you want to do with it. Since jar-files are a special form of packed zipfiles, usually containing a Manifest and the classes, the Archivmanager isn't a false solution, and it is saver to show the content of the archive, than executing it.

Copying the file to windows has no effect. Windows not even has an executable flag, but you shouldn't fiddle with it though. You change your desktop settings, and those can't be moved to windows, and you will not want to.

And if you have the correct settings in your DE, you don't need to tell Netbeans or any other IDE repeatedly, what to do with jar-files.

Jar files are basically a zip file, to create an executable, you have several different methods. The Best (in my opinion) is to use ant to create it. Or you can simply echo "Main-Class: YOUR.MAIN.CLASS" >> Manifest and then create your jar by jar -cmf Manifest JARFILENAME.jar INPUTFILES then, to make it executable under linux, right click on it and click on properties. Then click on permission tab and check execute . or you can be a terminal bamf and cd to the jar directory and chmod +x JARFILE.jar HAPPY NIXING!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM