如何找到正在运行的jar文件的路径

发布于 2024-11-01 15:34:34 字数 500 浏览 10 评论 0原文

我正在尝试以编程方式查找 jar 文件运行时的完整路径。我知道还有很多与此相关的其他问题,但似乎没有一个对我有用 - 最值得注意的是,我

MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().g­etPath()

多次偶然发现。在 Eclipse 中调试时,该特定方法适用于我,但是一旦我编译为 jar,它就会返回 NullPointerException。其他方法编译后也遇到类似问题。

我有一个使用 java.class.path 的临时解决方法,但是当我从 GUI 执行 jar 时,它只返回完整路径 - 在终端中,它失败了。

我还应该指出,我遇到此问题的唯一系统是 Linux。在 Windows 和 Mac 上,我没有遇到任何问题。

任何帮助将不胜感激:)

谢谢!

德里克

编辑:jar 是可执行的,如果这改变了什么。

I'm trying to programmatically find the full path of a jar file while it's running. I know there are a number of other questions about this, but none of them seem to work for me - most notably, I've stumbled across

MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().g­etPath()

a number of times. That particular method works for me when debugging in Eclipse, but once I compile to a jar, it returns a NullPointerException. Other methods have met similar problems after compiling.

I have a temporary workaround by using java.class.path, but that only returns the full path when I execute the jar from the GUI - in the terminal, it fails.

I should also note that the only system that I'm having this problem on is Linux. On Windows and Mac, I have no troubles.

Any help would be appreciated :)

Thanks!

Derek

EDIT: The jar is executable, if that changes anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2024-11-08 15:34:34

你做不到。类加载器不需要支持这一点,而且大多数也不支持。

或者,也许这个表述会更有帮助。二进制类通过 ClassLoader 对象进入 JVM。 ClassLoader 对象不需要跟踪它们加载的类的来源。他们可以从任何地方加载它们:罐子、网络、数据库、旧锡罐。

因此,如果您想始终了解应用程序中类的出处,则必须始终使用类加载器加载代码,该类加载器确实会以对您有用的方式跟踪出处。

如果您控制整个应用程序,您就可以做到这一点。

如果您不控制整个应用程序,而是谈论加载到任意应用程序中的任意类加载器中的任意 jar,则不能依赖于了解其位置。

You can't do it. There is no requirement for ClassLoaders to support this, and most don't.

Or, perhaps this formulation would be more helpful. Binary classes come into the JVM via ClassLoader objects. ClassLoader objects are not required to keep any track of the provenance of the classes they load. And they can load them from anywhere: a jar, over the web, a database, an old tin can.

So, if you want to always know the provenance of classes in your application, you have to always load code with a class loader that, indeed, does track provenance in a manner useful to you.

If you control the entire application, you can do that.

If you don't control the entire application, and are rather talking about an arbitrary jar loaded into an arbitrary class loader in an arbitrary app, you can't depend on learning its location.

↙厌世 2024-11-08 15:34:34

即使从 jar 文件运行,以下内容也适用于我:

URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
String p = URLDecoder.decode(url.getFile(), "UTF-8");
File jarFile = new File(p);

通过 URLDecoder 发送路径很重要,因为否则如果目录包含空格,将创建其中包含 %20 的路径名。

The following works for me even when running from a jar file:

URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
String p = URLDecoder.decode(url.getFile(), "UTF-8");
File jarFile = new File(p);

Sending the path through the URLDecoder is important because otherwise a pathname with %20 in it will be created if the directory contains spaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文