1

I'm trying to load a resource (plain text file) from a second JAR that has not be loaded yet. This resource will contain a string representing a class in this second jar which I plan to use.

I'm having trouble finding the correct way to load this resource, and previous similar questions haven't gotten me much further. Here is what I'm working with:

public void readResource() { ClassLoader loader = Thread.currentThread().getContextClassLoader(); } 

I can see in this ClassLoader (which ends up being a WebappClassLoader) has the list of jars in the directory:

jarNames: [com.mysql.jdbc.jar, productivity-common.jar] jarPath: /WEB-INF/lib 

When I try to load up the file using the ClassLoader, I'm getting a NullPointerException:

String path = loader.getResource("com/productivity/common/META-INF/providers/hello.txt").getPath(); 

If this would work, my next step would be reading the value in this file using an InputStream, and trying to create a new instance of a class matching that value from the same second jar. From what I'm reading, I would use the path to that class and use Class.forName("value").newInstance(), but I'm not confident that's right either.

Any assistance would be greatly appreciated. I'm trying to learn how ClassLoaders work and writing this (what should be simple) project to help.

4
  • Do you have both file & path identical? means com/test/abc.properties in both jar file? Commented Dec 21, 2014 at 17:42
  • My file is only in one jar. My first jar's package is com.test.forecaster which contains no resources. This is the main jar that my webapp via Tomcat calls into. My second jar is com.productivity.common which contains src/main/resources/META-INF/providers/hello.txt Commented Dec 21, 2014 at 17:50
  • I answered question below. Tricky part is to provide correct directory structure in getResources. just open the jar file and find out the path. Here my assumption is "META-INF/providers/hello.txt" will work. Commented Dec 21, 2014 at 17:53
  • I just learned another interesting thing. If you are in mac or unix you can just try to open jar file in "vi", it will show all the files with path. Example vi test.jar Commented Dec 21, 2014 at 17:58

1 Answer 1

2

Let me assume you have two resource files with same name "spring/label.properties" stored in two different jar files.

You can use following code to find list of all files from class path, then filter based on path.

 Enumeration<URL> en = this.getClass().getClassLoader().getResources("spring/label.properties"); while(en.hasMoreElements()){ URL url = en.nextElement(); //Print all path to visualize the path System.out.println(url.getPath()); if(url.getPath().contains("my-jar")){ // This can be jar name BufferedReader reader = new BufferedReader(new InputStreamReader(en.nextElement().openStream())); String str = null; while((str = reader.readLine())!=null){ // Now you can do anything with the content. System.out.println(str); } } } 

Does that help?

Sign up to request clarification or add additional context in comments.

3 Comments

Just tried this, 'en' ends up being empty. I've tried both the current class's classloader as well as the current thread's context classloader, getResources() pointing to "META-INF/providers/hello.txt'. I even added the same text file to my 'main' jar in the path WebContent/META-INF/providers/hello.txt, changed the path in the getResources(), and that still causes an empty enumeration.
Have you tried opening jar and inspecting the content?
Wow... I had it right all along, apparently when I exported my second project into a JAR it was before I had added my resource file. Sigh.. thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.