I have a directory /plugin with two jar in there A.jar and B.jar .Both has a file with same name, 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. How to solve this conflict?

有帮助吗?

解决方案

Use ClassLoader#getResources() to get URLs of all config.xml files. 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

其他提示

put the one with the latest files before the other one in the classpath

Are you sure it isn't loading both config.xml files? 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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top