We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,439 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Resources in JAR file

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!

3
Contributors
6
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
end3r
Light Poster
41 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 17

Yes. It's there.

end3r
Light Poster
41 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 17

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");
end3r
Light Poster
41 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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") ?

~s.o.s~
Failure as a human
Administrator
12,247 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 787
Skill Endorsements: 55

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!

end3r
Light Poster
41 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by masijade and ~s.o.s~

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0859 seconds using 2.66MB