简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM