简体   繁体   English

从GNOME运行时如何获取可运行.jar文件的位置

[英]How to get location of runnable .jar File when run from GNOME

I am developing a small Java application using Swing, that is supposed to run on Windows/Linux and MacOS. 我正在使用Swing开发一个小型Java应用程序,该应用程序应该在Windows / Linux和MacOS上运行。

Eventually it will ship as a runnable jar with some config files in the same folder. 最终,它将作为可运行的jar发行,在同一文件夹中带有一些配置文件。 To load them I need the path to the folder the jar is stored in within the program. 要加载它们,我需要将jar文件存储在程序内的文件夹的路径。
There are already a couple of threads like this one or this one. 已经有几个像线程的这一个这一个。

My problem is, that all the solutions discussed there work fine, when I run the program from within eclipse or call the runnable jar from a terminal like so: 我的问题是,当我在eclipse中运行程序或从终端调用可运行的jar时,这里讨论的所有解决方案都可以正常工作:

java -jar /path/to/jar/jarfile.jar

However when I click on the jar file in Cinnamon or Gnome (which is what most of the users will know to do), I do not get the correct paths. 但是,当我单击Cinnamon或Gnome中的jar文件(这是大多数用户会做的事情)时,我没有得到正确的路径。 (MacOS users report the same issue) (MacOS用户报告相同的问题)

Here is what I've tried so far and what the output is when run via double click (all those display the desired path when run from eclipse or a terminal): 这是我到目前为止已经尝试过的东西,以及通过双击运行时的输出(从eclipse或终端运行时,所有输出均显示所需的路径):

ClassLoader.getSystemClassLoader().getResource(".").getPath()
Output: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/

SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Output: ./

System.getProperty("user.dir");
Output: /home/myusername

Is there any other way to do it or am I doing something wrong when exporting the jar? 还有其他方法可以执行此操作,或者在导出罐子时我做错了什么? Any help would be appreciated! 任何帮助,将不胜感激!

Cheers Nick 干杯尼克

Make it simple, and use a startup script (.bat/.sh file) to run your application. 使其简单,并使用启动脚本(.bat / .sh文件)运行您的应用程序。 This startup script will get the path of its own location in the filesystem, and pass it as an argument or system property to the Java application. 该启动脚本将获取文件系统中其自身位置的路径,并将其作为参数或系统属性传递给Java应用程序。 This has the additional advantage of being able to pass other arguments, like the size of the heap, etc. 这具有能够传递其他参数(如堆的大小等)的附加优点。

On windows, %~dp0 is the path of the directory containing the executed bat file. 在Windows上, %~dp0是包含已执行的bat文件的目录的路径。 On Unix, you can use $(dirname $0) . 在Unix上,可以使用$(dirname $0)

You could store all config files as resources in a jar, and copy them to files in home.dir + ".AppName/". 您可以将所有配置文件作为资源存储在jar中,并将它们复制到home.dir +“ .AppName /”中的文件中。

Painful as it is, the Preferences API , Preferences.systemNodeForPackage , seems the wisest alternative, if there is little structured config data. 尽管很痛苦,但如果结构化配置数据很少的话, Preferences API Preferences.systemNodeForPackage似乎是最明智的选择。 There is an inputStream method for import; 有一个用于导入的inputStream方法; your initial config template could be a resource in the jar. 您的初始配置模板可能是jar中的资源。

Just get the class path using System.getProperty("java.class.path") and scan it for your ".jar" name. 只需使用System.getProperty(“ java.class.path”)获取类路径,然后扫描它的名称即可。 Note that path separators are OS dependent (File.pathSeparator) 请注意,路径分隔符取决于操作系统(File.pathSeparator)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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