简体   繁体   English

在jar中访问pom.xml

[英]Accessing to pom.xml in jar

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

} }

now it prints out where the pom.xml located in jar file. 现在它将打印出jar文件中pom.xml所在的位置。 META-INF/maven/~~~~/~~~~/pom.xml META-INF / maven / ~~~~ / ~~~~ / pom.xml

And now I want to access to pom.xml file. 现在,我想访问pom.xml文件。 I did some research and I think I need to use one of below 我做了一些研究,我认为我需要使用以下一项

InputStream is = obj.getClass().getClassLoader().getResourceAsStream(pomPath);

when calling this, I don't know what to use for the calling obj, so I just set up null string as a calling object and it says null pointer exception. 调用此方法时,我不知道用于调用obj的内容,因此我只是将null字符串设置为调用对象,并显示null指针异常。

Also, I saw somewhere that I need to use JarInputStream to access JarFile . 另外,我看到某个地方需要使用JarInputStream访问JarFile But I have no idea using it. 但是我不知道使用它。 Help me! 帮我!

Say you are in the class of the main, MainClass: 假设您属于MainClass主类:

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

Mind that the path must start with a slash (absolute path), otherwise it would be relative to the package directory of that class. 注意,该路径必须以斜杠(绝对路径)开头,否则它将相对于该类的package目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM