在 Linux 中执行 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6724953/
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:07:10  来源:igfitidea点击:

Executing a jar file in Linux

javalinuxjar

提问by Pankaj Singhal

I have created an executable Java .jar file. That is to say, I properly packaged a java program inside a .jar file, including a META-INF/MANIFEST file.

我创建了一个可执行的 Java .jar 文件。也就是说,我把java程序正确打包在一个.jar文件中,包括一个META-INF/MANIFEST文件

(Incidentally, on most Windows machines with Java installed, .jar files are registered to run as something like javaw.exe -jar %1, so you can just double click jars that have been prepared in that way.)

(顺便说一句,在大多数安装了 Java 的 Windows 机器上,.jar 文件被注册为以类似的方式运行javaw.exe -jar %1,因此您只需双击以这种方式准备的 jar。)

I just copied the same file into Linux and tried to do the same thing.

我只是将同一个文件复制到 Linux 中并尝试做同样的事情。

But, when I click on that file it is just opening the jar as a zip file, rather than invoking java.

但是,当我单击该文件时,它只是将 jar 作为 zip 文件打开,而不是调用 java.lang.

How can I configure my Linux system so that the program inside the jar executes when I double click on the jar file? That is, how can I make my system invoke something like java -jar test.jarwhen I double click test.jar?

如何配置我的 Linux 系统,以便在双击 jar 文件时执行 jar 中的程序?也就是说,如何让我的系统java -jar test.jar在双击 test.jar 时调用类似的东西?

采纳答案by Ryan Stewart

Heh, what @marc is trying to say in his comment is that how you do that could be wildly different depending on your linux flavor. In general, you just need to find out how to change the default "Open With" behavior for whichever flavor you're using. You need to tell it to open jar files with Java instead of with the archive program.

呵呵,@marc 在他的评论中想说的是,根据您的 linux 风格,您如何做到这一点可能会大不相同。通常,您只需要了解如何更改您使用的任何风格的默认“打开方式”行为。您需要告诉它使用 Java 而不是归档程序打开 jar 文件。

回答by mikey

Personally, I'd leave the jar file as is and create a script that will execute the command when it is clicked, then I'd just click on that.

就个人而言,我会保留 jar 文件并创建一个脚本,该脚本将在单击时执行该命令,然后我只需单击它。

What window manager / desktop are you using in linux? You can likely configure the default action of a click on a jar file.

你在 linux 中使用什么窗口管理器/桌面?您可能会配置单击 jar 文件的默认操作。

回答by Dylan

One possible way would be to make a text file with that command java -jar test.jarin it, then give that file execute permissions. Name it testThen you would double click that file (or via the command line just type ./test)

一种可能的方法是制作一个包含该命令的文本文件java -jar test.jar,然后授予该文件执行权限。命名test然后您将双击该文件(或通过命令行键入./test

回答by 0xAX

Make your file executable:

使您的文件可执行:

sudo chmod +x test.jar