Reputation: 6391
If I run the following code within eclipse my program compiles with no issue. If I try to export the program as a runnable jar file my resource cannot be found.
public class Test {
public static void main(String[] args) {
File file = new File(Tester.class.getClassLoader().getResource("res/myFile.txt").getFile());
if(!file.exists()) { System.out.println("File not found."); }
}
}
Folder structure (by running jar tf test.jar
):
com/
com/test/
com/test/Tester.class
res/
res/myFile.txt
Upvotes: 0
Views: 917
Reputation: 57381
There is no such file in case of jar.
Use instead Tester.class.getResourceAsStream("/myFile.txt")
and work with the stream
Upvotes: 1