1.11M Members

Resources in JAR file

 
0
 

Hi,

I have a problem with some resource files (i.e. .txt) which I have created in the project under let's say com/myapp/resources.

I have a class created: com.myapp.util.MyClass which reference a file.txt located in resources folder:

MyClass.class.getResourceAsStream("../resources/file.txt");

The project is created in eclipse. After I export the project as runnable jar I try to execute the jar and I get a null InputStream.

Could you guys please tell me what else is to be configured (something in the MANIFEST file probably... :| )

Thanks in advance!

 
0
 

Did you check that the file is actually in the created jar?

 
0
 

Yes. It's there.

 
0
 

All I can say at the moment is check your spelling. On windows when not packaged in a jar it will not be case sensitive, but once is packed into the jar it is case sensitive.

 
0
 

Got the solution at last. The path to the file was incorrect.
It seems that the path has to begin with a '/'. So instead of

MyClass.class.getResourceAsStream("com/<path>/resources/file.txt");

I will have

MyClass.class.getResourceAsStream("/com/<path>/resources/file.txt");
 
0
 

Any specific reason you want to load resource relative to a class and not relative to a classloader using MyClass.class.getClassLoader().getResourceAsStream("my/pkg/file.txt") ?

 
0
 

Nope. It was just a scenario. But you are right. A resource should be referenced by root folder and not related to a class. It's more general.

Thanks!

Question Answered as of 2 Years Ago by masijade and ~s.o.s~
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: