gpt4 book ai didi

java - 在 OS X 上读取 Jar 的 list

转载 作者:行者123 更新时间:2023-11-30 03:45:33 25 4
gpt4 key购买 nike

我的 jar 文件中有以下 list :

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.3
Created-By: 1.7.0_67-b01 (Oracle Corporation)
Main-Class: tld.project.Example

Name: tld/project
Specification-Title: Example
Specification-Version: 1.0
Specification-Vendor: Me
Implementation-Title: tld.project
Implementation-Version: 20140913
Implementation-Vendor: Me

我使用以下命令在运行时从 list 中读取一些属性:

// Try and load the Jar manifest as a resource stream.
InputStream manInputStream = Example.class.getResourceAsStream("/META-INF/MANIFEST.MF");
if (manInputStream != null) {
// Try and extract a version string from the Manifest.
Manifest manifest = new Manifest(manInputStream);

Attributes attributes = manifest.getAttributes("tld/project");

为什么上面的代码将属性设置为 null,但仅在 OS X 上? (1.7.51) 如果我检查 manifest.getEntries().size() 它也会返回零,但同样,这在 Windows 和 Linux 上按预期工作。有什么想法吗?

最佳答案

Example.class.getResourceAsStream 不一定会在包含 Example.class 的同一个 jar 文件中查找,它会搜索该类上所有可用的 JAR loader 并为您提供它找到的第一个匹配资源。这不是 Mac OS 特定的,在所有平台上都是相同的规则,但当您在 ​​Mac 上运行时,您的类路径的顺序可能与在 Windows 或 Linux 上运行时的顺序不同,并且您从不同的 jar 中获取 list 。 p>

您可以使用 getResource 查看您实际加载的 list 。

如果您特别想要包含 Example.class 的 jar,您可以使用

找到它
Example.class.getProtectionDomain().getCodeSource().getLocation()

关于java - 在 OS X 上读取 Jar 的 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25828401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com