如何获取我的应用程序使用的所有 jar 名称?
根据下图,我想要一个包含所有 jar 文件名的数组,如下所示:
myArray = ["log4j-1.2.17.jar","commons-io-2.4.jar","zip4j_1.3.2.jar"]
我已阅读this question ,然后尝试这个:
String classpath = System.getProperty("java.class.path");
log.info(classpath);
List<String> entries = Arrays.asList(classpath.split(System.getProperty("path.separator")));
log.info("Entries " + entries);
但是当我运行 jar 时,我在日志文件中得到了以下内容:
2015-07-10 17:41:23 INFO Updater:104 - C:\Prod\lib\Updater.jar
2015-07-10 17:41:23 INFO Updater:106 - Entries [C:\Prod\lib\Updater.jar]
下面是同样的问题,其中一个答案说我可以使用 Manifest 类,但我该怎么做?
最佳答案
您可以像这样使用 list 条目:
Enumeration<URL> resources = getClass().getClassLoader()
.getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
try {
Manifest manifest = new Manifest(resources.nextElement().openStream());
// check that this is your manifest and do what you need or get the next one
...
} catch (IOException E) {
// handle
}
}
这是一个有关阅读 Manifest 全文的问题
从那里,您可以获得所有依赖项名称。
关于java - 获取应用程序使用的jar库名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352000/