Lukasz Medza
Lukasz Medza

Reputation: 499

JAR ignores files inside it

I have a program that I want to save as a JAR, and so I expert it using eclipse. When I open it as an archive I can clearly see that my text files, but when I try to run the JAR from the terminal it throws a file not found exception. Am I missing something obvious here?

Upvotes: 0

Views: 35

Answers (1)

Powerlord
Powerlord

Reputation: 88796

To access files inside a JAR, you need to use the Class.getResource or Class.getResourceAsStream methods as they aren't visible to normal file operations.

Note that these aren't static methods (I was thinking they were, whoops), so you'll probably want something like...

InputStream in = this.getClass().getResourceAsStream("path/to/textfile");

Note that web applications have their own versions of these methods in the ServletContext class.

Upvotes: 2

Related Questions