1

I have a spring application with multiple dependent libraries that have the inner properties/xml that I want to read.

  • test-app.jar
    • dep1.jar
    • dep2.jar
    • dep3.jar ...

Each of the dep1/2/3 jars have the file called META-INF/config.properties which contains the files to further read within that dependent jars.

I tried the ResourceUtils.getURL("classpath:/META-INF/config.properties"), but it always reads from the first dependent file.

How can I read from each jars that contains the same name?

Se Hee Lee
  • 11
  • 2

1 Answers1

1

I found this solution after searching:

final Enumeration<URL> resEnum =
        MyClass.class.getClassLoader()
            .getResources("META-INF/config.properties");
final List<URL> resources =
        Collections.list(resEnum);
//Copied from riptutorial.com

Refs:
https://riptutorial.com/java/example/19290/loading-same-name-resource-from-multiple-jars
https://stackoverflow.com/a/6730897


Updated solution:

package abc;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

public class MultipleClasspathEntries {
    public static void main(String[] args) throws IOException {
        final Enumeration<URL> resEnum =
                MultipleClasspathEntries.class.getClassLoader()
                        .getResources("META-INF/MANIFEST.MF");

        while (resEnum.hasMoreElements()) {
            System.out.println(resEnum.nextElement());
        }
    }
}
//Copied from riptutorial.com

$JAVA_HOME/bin/javac abc/*.java

$JAVA_HOME/bin/java -cp /c/so-67847004/commons-lang-2.4.jar:/c/so-67847004/commons-collections-3.2.1.jar:. abc.MultipleClasspathEntries

Output:

jar:file:/C:/so-67847004/commons-lang-2.4.jar!/META-INF/MANIFEST.MF
jar:file:/C:/so-67847004/commons-collections-3.2.1.jar!/META-INF/MANIFEST.MF
Andrew NS Yeow
  • 341
  • 2
  • 8