1,105,229 Community Members

Resources in JAR file

Member Avatar
end3r
Light Poster
41 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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

Member Avatar
end3r
Light Poster
41 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes. It's there.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
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.

Member Avatar
end3r
Light Poster
41 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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");
Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
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") ?

Member Avatar
end3r
Light Poster
41 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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: