如何从清单的类路径条目中获取 jar
我有一个可运行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要使用 Manifest 读取 MANIFEST.MF 文件并使用该类提取 Class-Path 属性的类
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
您实际上需要 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 staticgetSystemClassLoader()
method, and the staticjava.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.