首页 > 解决方案 > 访问 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. 但我不知道使用它。帮我!

标签: javaxmlparsingjarinputstream

解决方案


假设你在主类 MainClass 中:

MainClass.class.getResourceAsStream("/....");

请注意,路径必须以斜杠(绝对路径)开头,否则它将相对于该类的包目录。


推荐阅读