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?
Asked
Active
Viewed 33 times
0
-
1We're going to need to see some of your code to help you debug it for you, as well as the stack trace and the lines of code it relates to, although I imagine it's a case of relative vs absolute addresses. – Rudi Kershaw Apr 28 '14 at 15:26
1 Answers
2
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.

Powerlord
- 87,612
- 17
- 125
- 175
-
+1 Because this is likely the case, but I can't help but feel like it's a bit of a leap of logic given how little information we're given in the question. – Rudi Kershaw Apr 28 '14 at 15:30
-
Updated my answer because I was dumb and forgot that these methods aren't static. – Powerlord Apr 28 '14 at 15:31
-
-
@Rudi True, and I considered putting that, but I wasn't sure how well that works in abstract classes. – Powerlord Apr 28 '14 at 15:39