javajarname-conflict

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

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?


Solution

  • 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