aspose file tools*
The moose likes Java in General and the fly likes Loading resource from different jar on classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loading resource from different jar on classpath" Watch "Loading resource from different jar on classpath" New topic
Author

Loading resource from different jar on classpath

John Coss
Greenhorn

Joined: Jul 30, 2012
Posts: 7
Hi,

I have a problem loading resource file from external jar file. Here is the situation:
I created a project, called "schemas", in Eclipse using maven, and under src/main/resources placed some files under com.schemas package. These are xml files, but I think that that is not important. Now I created another project, called "execution" in Eclipse also with maven and written some code that access xml files from "schemas" project. I placed dependencies in maven from schemas project. I use following code to access Inputstream of given resource:

When I run this code within Eclipse it works fine. However when I bundle these to jars (schemas.jar, execution.jar), using maven or Eclipse, and run (execution.jar as runnable jar) it from command line, not Eclipse, I have null pointer exception when loading xml file (returned InputStream is null). Execution.jar has no problems with loading classes from schemas.jar as they are on the classpath. If I place these xml files into Execution.jar project then I have no problems.
Is there a way to access these resources placed in schemas.jar from execution.jar using classpath identifiers? I googled that there are solutions that loads JAR file as zip file and reads contents of jar. Aren't jar resources in jar files automatically placed on classpath or only compiled java code, class files, are available on classpath.
Here is the situation that I want:
I have classpath structure
com.schemas.xsdtestxml.xml
com.schemas.loader.class
Physically xsdtestxml.xml resides in schemas.jar, while loader.class resides in execution.jar, but they are both in same classpath and loaded during startup. Now I would like to access xsdtestxml.xml from loader.class. Is that possible, or only class files are available on classpath? Like I said, all this works perfectly in Eclipse.

Thanks in advance!


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Welcome to JavaRanch, John.

If I understand correct you created two projects in Eclipse, one containing the XML and the other with Loader class. Now you try to export both projects as independent jar files and try to run using the command prompt. In that case, you need to add both the jars in the class path and execute the below command - where com.schemas.Loader will be your Loader class.

java -cp execution.jar;schemas.jar com.schemas.Loader

Another way of doing it is to use the "Export-> Executable Jar file" option to package the necessary XML files into the jar itself. And then you can run the executable jar file using the command - java -jar execution.jar
John Coss
Greenhorn

Joined: Jul 30, 2012
Posts: 7
Hi John and thank you for your welcoming post

Thanks for your reply, but that was not what I was searching for. I tried to run jar with your command, but I still have same results: cannot load resource file from jar other then jar where static main resides.
Problem is, that I have loader class in execution.jar that loads xml file (type of file is not important) from schemas.jar. I use:

to load that file and I have no problem with that within Eclipse. However, when I export these to jar from Eclipse (or using maven package), I get nullpointer exception since that getResourceAsStream cannot read file from schemas,jar.
Interestingly enough, I can read .class files in raw format from schemas.jar (I have placed for the sake of testing some code there). It seems that any type of file, other then .class, is unavailable from jars other then jar where main method is. For some reason, I have no problem accessing resource files placed in execution.jar, but I can't access any file (other then class) from schemas.jar. I renamed schemas.jar to schemas.zip and those xml files are physically available in schemas.jar, but not loaded on classpath.
My conclusion, for the time being is, that on classpath only resources from jar than contains requested static main method are loaded. Resources from other jar files are unavailable, except for java compiled class files.
Perhaps there is an explicit command that tells jvm to make resource files available on classpath. Maybe Eclipse, for testing purposes, packs compiled files in different manner, or even has its own classloader that can access those resource files.

Any ideas now?
Thanks in advance
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
John Coss wrote:My conclusion, for the time being is, that on classpath only resources from jar than contains requested static main method are loaded. Resources from other jar files are unavailable, except for java compiled class files.

I would disagree, as I was able to load XML files from other jars using your getResourceAsStream(). Hopefully you checked the XML file is present in the other jar file and can create a test class in the schemas.jar to verify.
John Coss
Greenhorn

Joined: Jul 30, 2012
Posts: 7
Ok it works now.

I have no idea how this happened, but it works (for now). I placed test in schema.jar and tried to display contents of xml file. It worked, then I created jar, and it worked for schema.jar. Next, I removed that test from schema.jar and tried to load that file from execution.jar. It worked in Eclipse, then I created execution.jar and schema.jar and now it works!?!?. Why it didn't worked before I don't know. Perhaps it is some error in java jar packer, or even maven didn't done its job correctly.
I wish that I have some information to help other people, since that google shows that this question was somehow common.
Here are steps that might help someone:
Added test to schema.jar
Executed test in schema.jar
Removed test from schema.jar
Executed clean install of schema.jar to maven repository
Executed test in execution.jar

There is a possibility that this was maven error, but I cannot be sure. I will have to see if this problem appears again in future.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading resource from different jar on classpath