在 Linux 上运行 .jar 文件

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

Running .jar File on Linux

linuxjardirectoryjava

提问by John Roberts

I have a .jar file that reads two files from within its current folder and produces as output a .txt file and a separate folder with multiple other .txt files. This works perfectly in Windows using this code to create the directory:

我有一个 .jar 文件,它从其当前文件夹中读取两个文件,并生成一个 .txt 文件和一个包含多个其他 .txt 文件的单独文件夹作为输出。这在使用此代码创建目录的 Windows 中非常有效:

static String dir = System.getProperty("user.dir");

I used the instructions here: https://askubuntu.com/questions/192914/how-run-a-jar-file-with-a-double-clickto set up my .jar file to run on a simple double-click, but as of right now, it does nothing when double-clicked. My guess is that the above line of code does not translate well to Linux. Anybody know how to resolve this?

我使用了这里的说明:https: //askubuntu.com/questions/192914/how-run-a-jar-file-with-a-double-click设置我的 .jar 文件以通过简单的双击运行,但截至目前,双击时它什么也不做。我的猜测是上面的代码行不能很好地转换到 Linux。有谁知道如何解决这个问题?

采纳答案by ktm5124

First, try running it on the command-line, with

首先,尝试在命令行上运行它,使用

java -jar <file.jar>

java -jar <file.jar>

The user.dirproperty is cross-platform (see here) so it should not be the problem. However, are you using correct file separators? Remember it's '/' on UNIX and '\' on Windows.

user.dir属性是跨平台的(请参见此处),因此应该不是问题。但是,您是否使用了正确的文件分隔符?请记住,它在 UNIX 上是“/”,在 Windows 上是“\”。

回答by Rajesh Babu

Try java -jar Jarname.jarand pass other files as arguments after this command

尝试java -jar Jarname.jar并在此命令之后将其他文件作为参数传递

回答by Penfold

The code line you gave works fine on linux.

您提供的代码行在 linux 上运行良好。

My best guess is that you're then trying to use this directory path by adding a windows-specific path separator (like path + "\subdir") which isn't appropriate for linux (you should build a new File object instead).

我最好的猜测是,您然后尝试通过添加不适合 linux 的特定于 Windows 的路径分隔符(如路径 + "\subdir")来使用此目录路径(您应该构建一个新的 File 对象)。

Either that, or your jar file isn't being executed at all. Have you tried doing something very simple in your jar file to see if anything is being run? Have you tried running your jar with java -jar myapp.jarto see if any exceptions are thrown or error messages displayed?

要么,要么根本没有执行您的 jar 文件。你有没有试过在你的 jar 文件中做一些非常简单的事情来看看是否正在运行任何东西?您是否尝试过运行 jarjava -jar myapp.jar以查看是否抛出任何异常或显示错误消息?

回答by Jeevan Patil

You will need to manually tweak your build process to get the jar file marked as executable. In your build xml file, 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.

您将需要手动调整构建过程以将 jar 文件标记为可执行文件。在您的构建 xml 文件中,有一个目标“-post-jar”,在构建 jar 后调用。您需要创建该目标并使用 Ant 的 chmod 任务来修改您的 jar。一旦你这样做了,每次你在那个项目中制作一个 jar 文件时都会发生。

It will run fine as long as you have a JRE installed.

只要您安装了 JRE,它就会运行良好。

Read this article to know more.

阅读本文以了解更多信息