我如何从manifest的Class-Path-entries中获取jar [英] How do i get jars from manifest's Class-Path-entries

查看:188
本文介绍了我如何从manifest的Class-Path-entries中获取jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的清单文件的Class-Path条目中有一个带有两个jar的runnable jar:

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

程序运行正常,并且满足引用的jar中的所有依赖项。但是,当我尝试访问类路径时,jar不存在:

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);
}

仅提供

Entry: .\module2-0.0.1-SNAPSHOT.jar

有没有办法访问实际的类路径,因为很明显,系统会在路径上找到这些jar?

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

推荐答案

你真的需要JAR文件的位置,还是只需要从中加载资源?

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

如果你真的只是想加载资源,那么你将是感兴趣的 java.lang.ClassLoader中,其静态 getSystemClassLoader()方法,和静态 java.lang.Thread.currentThread()。getContextClassLoader()方法。

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.

如果你想找到他们是因为你想读Jar文件名的版本字符串......如果它们在你的控制下无论如何......你可能想要使用 Package.getPackage(my.package)。getImplementationVersion()并组织以获得所需的值wr将它放入组件罐的清单中。

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.

这篇关于我如何从manifest的Class-Path-entries中获取jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆