简体   繁体   English

jar文件中的名称冲突

[英]Name conflicts in jar file

I have a directory /plugin with two jar in there A.jar and B.jar .Both has a file with same name, config.xml 我有一个目录/ plugin,其中有两个jar,分别是A.jar和B.jar。两者都有一个同名的文件config.xml

File file = new File("plugin/");
for (File item: file.listFiles()) {

    if (item.isFile() && item.getName().substring(item.getName().lastIndexOf(".")).equals(".jar")) {
        ClassPathHacker.addFile(item); // classPathHacker adds the jar to classpath

        InputStream is = getClass().getClassLoader().getResourceAsStream("config.xml");
    }
}

But the problem is it is not loading the new config.xml file after a new iteration rather loading the first config.xml each time. 但是问题在于它不是在新的迭代之后加载新的config.xml文件,而是每次加载第一个config.xml。 How to solve this conflict? 如何解决这个矛盾?

将具有最新文件的文件放置在类路径中的另一个文件之前

Are you sure it isn't loading both config.xml files? 您确定它不会同时加载两个config.xml文件吗? Remember that when resolving names processing starts at the left-hand side of the PATH/CLASSPATH and moves one token at a time to the right until either the end of the list is reached or the first match is found. 请记住,在解析名称时,处理始于PATH / CLASSPATH的左侧,并一次向右移动一个标记,直到到达列表的末尾或找到第一个匹配项为止。

Use ClassLoader#getResources() to get URLs of all config.xml files. 使用ClassLoader#getResources()获取所有config.xml文件的URL。 Then you can choose which one to use, probably you'll need the last one. 然后,您可以选择使用哪一个,可能需要最后一个。

Another option is not to mess with the classloader hacks and just read the file you need directly from the .jar using ZipFile 另一个选择是不要弄乱classloader的hack,而只是使用ZipFile从.jar中直接读取您需要的文件

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

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