Linux Ubuntu (NetBeans) 上的可执行 JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6949348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:29:13  来源:igfitidea点击:

Executable JAR on Ubuntu (NetBeans)

javalinuxnetbeansubuntuexecutable-jar

提问by Sarah

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

我正在 NetBeans 中编写一个简单的 Swing 应用程序,并且是第一次在 Ubuntu 机器上这样做。

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

众所周知,NetBeans 会自动为“设置为主”的项目创建可执行 JAR。

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.

在 Windows 上,您可以双击可执行 JAR,它会自动调用 JRE 并运行应用程序。在 Ubuntu 中,双击 .jar 文件会导致在存档管理器中打开该文件。为了运行我的 JAR,我必须右键单击它并选择“使用 OpenJDK Java 6 运行时打开”或从命令行启动它。

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

文件 MySwingApp.jar 未标记为可执行文件...

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
  1. 我该怎么做才能将其设置为可执行文件?这是我可以在NB内部做的事情还是我必须使用shell?如果我必须通过 shell 设置权限,这是否与 NB 自动生成 **executable** JARS 的策略冲突?无论如何,我将使用什么命令来翻转可执行位?!?!
  2. 这只是一个 Linux 小问题吗?我想将此 JAR 发送给运行 Windows 的朋友,我希望他们能够双击它并启动程序

Thanks for any helpful suggestions!

感谢您提供任何有用的建议!

采纳答案by Paul

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

  1. 您将需要手动调整构建过程,以便在 Netbeans 中将 jar 文件标记为可执行文件。转到您的项目根目录并打开 build.xml。标题有关于添加到构建过程的说明。有一个目标,“-post-jar”,在构建 jar 之后调用。您需要创建该目标并使用Ant 的 chmod任务来修改您的 jar。一旦你这样做了,每次你在那个项目中制作一个 jar 文件时都会发生。

  2. 只要他安装了 JRE,它就可以在您朋友的 Windows 机器上正常运行。

Here is a threadabout running jars using double click in Linux.

这是一个关于在 Linux 中使用双击运行 jars的线程

回答by user unknown

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

Jar 文件不是一流的可执行文件,它们不会通过更改其可执行标志而变成神奇的可执行文件。

If you execute a jar, you run the command

如果执行 jar,则运行命令

 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

是一样的,就好像你双击一个png文件,并期望它在绘画程序中运行

 gimp YOUR.png

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

你不需要让你的 png 成为可执行文件,它不会解决任何问题。

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.

相反,您必须告诉您的桌面环境,双击 jar 或 png 文件时要做什么,并且您必须在 Linux 上以与在 Windows 中相同的方式执行此操作-也许 Windows 上的安装程序会为您执行此操作,因为操作系统 (Windows) 上通常只有桌面环境 (Windows),但 Linux 有 Gnome、KDE、XFCE、LXDE、fluxbox 等等。

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.

并不确定你想用它做什么。由于 jar 文件是一种特殊形式的压缩 zip 文件,通常包含一个清单和类,Archivmanager 不是一个错误的解决方案,它比执行它更能显示存档的内容。

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.

将文件复制到 Windows 没有任何效果。Windows 甚至没有可执行标志,但你不应该摆弄它。您更改了桌面设置,而这些设置无法移动到 Windows,您也不会想要。

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.

如果您在 DE 中有正确的设置,则无需反复告诉 Netbeans 或任何其他 IDE 如何处理 jar 文件。

回答by alexmherrmann

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" >> Manifestand then create your jar by jar -cmf Manifest JARFILENAME.jar INPUTFILESthen, 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.jarHAPPY NIXING!!

Jar 文件基本上是一个 zip 文件,要创建可执行文件,您有几种不同的方法。最好的(在我看来)是使用 ant 来创建它。或者您可以简单地echo "Main-Class: YOUR.MAIN.CLASS" >> Manifest然后创建您的jar jar -cmf Manifest JARFILENAME.jar INPUTFILES,使其在linux下可执行,右键单击它并单击属性。然后单击权限选项卡并检查execute。或者你可以终端 bamf 和 cd 到 jar 目录和chmod +x JARFILE.jarHAPPY NIXING!!

回答by Parth

I gone through Internet and I came across one article with complete steps to run jar file http://mlartist.blogspot.in/2012/07/deployment-netbeans-project-in-linux.html

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

回答by David

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.

您可以像 Ubuntu(和其他 linux)上的本机二进制文件一样使用 Java,这是内核的一个特性。您需要安装 binfmt-support 包,以便为内核提供以这种方式运行 java 的钩子。

In Ubuntu open up a terminal and run:

在 Ubuntu 中打开一个终端并运行:

sudo apt-get install binfmt-support

sudo apt-get install binfmt-support

Then make your JAR file executable

然后使您的 JAR 文件可执行

chmod a+x yourjar.jar

chmod a+x yourjar.jar

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

然后你可以像运行任何其他二进制文件一样运行你的 JAR

yourjar.jar

yourjar.jar