java - 访问 jar 中的 pom.xml
问题描述
public static void main(String[] args) throws IOException {
String filePath = args[0];
JarFile jar = new JarFile(new File(filePath));
Enumeration<JarEntry> zipEntries = jar.entries();
String pomPath = null;
for (JarEntry entry : Collections.list(zipEntries)) {
if (entry.getName().contains("pom.xml")) {
pomPath = entry.getName();
System.out.println(pomPath);
}
}
}
现在它打印出 pom.xml 在 jar 文件中的位置。META-INF/maven/~~~~/~~~~/pom.xml
现在我想访问 pom.xml 文件。我做了一些研究,我认为我需要使用以下之一
InputStream is = obj.getClass().getClassLoader().getResourceAsStream(pomPath);
调用它时,我不知道调用 obj 使用什么,所以我只是将空字符串设置为调用对象,它说空指针异常。
另外,我在某个地方看到了我需要用来JarInputStream
访问JarFile
. 但我不知道使用它。帮我!
解决方案
假设你在主类 MainClass 中:
MainClass.class.getResourceAsStream("/....");
请注意,路径必须以斜杠(绝对路径)开头,否则它将相对于该类的包目录。
推荐阅读
- unity3d - 辅助“呼吸”字体大小(使用协程增加和减少)
- java - 远程桌面会话 (RDP) 后 JavaFX 应用程序屏幕黑屏且无响应
- django - VueJS 中的 ERR_ABORTED 404(未找到)错误
- node.js - 通过数组 mongoose 中的 id 查找对象
- sybase - 可以在没有元数据访问器信息的情况下连接到 Sybase 数据库吗?
- javascript - Chrome/iOS 和 Chrome/MacOS 之间的不同 Javascript 行为
- python - 让两个数据库模型显示在一个 html 页面上
- javascript - 让下拉列表在 R Shiny 的 DT 数据表中的单元格内工作
- python - 是否可以将动态值传递给 dbt 源新鲜度测试?
- bash - 如何使 bash shell 脚本用 jq 解析 JSON 文件,然后将结果打印到 PHP 文件?