Question

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?

Was it helpful?

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top