如何从清单的类路径条目中获取 jar

发布于 2024-10-09 22:43:43 字数 599 浏览 1 评论 0原文

我有一个可运行的 jar,其清单文件的类路径条目中有两个 jar:

Class-Path: module1-0.0.1-SNAPSHOT.jar base-0.0.1-SNAPSHOT.jar
Main-Class: test.MySPI

程序运行良好,并且引用的 jar 中的所有依赖项都得到满足。但是,当我尝试访问类路径时,jars 不在那里:

String classpath = System.getProperty("java.class.path");
String[] entries = classpath.split(System.getProperty("path.separator"));
for (String entry : entries) {
    System.out.println("Entry: " + entry);
}

只给出

Entry: .\module2-0.0.1-SNAPSHOT.jar

Is there a way of access theactual classpath,因为显然,系统在路径上找到了这些 jars?

I have a runnable jar with two jars in the Class-Path entry of its manifest file:

Class-Path: module1-0.0.1-SNAPSHOT.jar base-0.0.1-SNAPSHOT.jar
Main-Class: test.MySPI

The program runs fine and all dependencies in the referenced jars are met. However, when I try to access the class path, the jars are not there:

String classpath = System.getProperty("java.class.path");
String[] entries = classpath.split(System.getProperty("path.separator"));
for (String entry : entries) {
    System.out.println("Entry: " + entry);
}

Only gives

Entry: .\module2-0.0.1-SNAPSHOT.jar

Is there a way of accessing the actual classpath, since obviously, the system finds those jars on the path?

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

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

发布评论

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

评论(2

你げ笑在眉眼 2024-10-16 22:43:43

I think you will need to use the Manifest class to read in the MANIFEST.MF file and extract the Class-Path attribute using that class

遗心遗梦遗幸福 2024-10-16 22:43:43

您实际上需要 JAR 文件的位置,还是只需要从中加载资源?

如果您实际上只想加载资源,那么您会对 java.lang.ClassLoader、其静态 getSystemClassLoader() 方法以及静态 java.lang.ClassLoader 感兴趣。 lang.Thread.currentThread().getContextClassLoader() 方法。

如果您想找到它们是什么,因为您想从 Jar 文件名中读取版本字符串...并且如果它们无论如何都在您的控制之下...您可能需要使用 Package.getPackage("my. package").getImplementationVersion() 并组织将所需的值写入组件 jar 的清单中。

Do you actually need the location of the JAR files, or do you just need to load resources from them?

If you actually just want to load resources, you'll be interested in java.lang.ClassLoader, its static getSystemClassLoader() method, and the static java.lang.Thread.currentThread().getContextClassLoader() method.

If you wanted to find what they were because you wanted to read the version string off the Jar file name ... and if they were under your control anyhow ... you might want to use Package.getPackage("my.package").getImplementationVersion() and organise to have the required values written into the Manifest of the component jars.

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