0

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?

Lukasz Medza
  • 469
  • 2
  • 8
  • 23
  • 1
    We'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 Answers1

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