Spring
Spring

Reputation: 11835

Java: How to package and access resources inside a runnable Jar file?

I have a Java console applicaton with a resources folder. It works OK, but when I want to export my code to a runnable Jar file(with Eclipse), then the exported Jar can not find the files(they are in buildpath) and give a Filenotfound exception.

When I unzip the exported Jar file I can see the files are there in the root folder, so I guess something wrong with my code.

BufferedReader srcFile = new BufferedReader(new FileReader("resources/"+filename));
String line = srcFile.readLine();

I tried to use

URL url= ClassLoader.getSystemResource(filename);
filename=url.tostring();        

But no luck.

Upvotes: 1

Views: 983

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

Use getResourceAsStream(String) (docs) if you want to read in a resource on the classpath.

Upvotes: 3

Related Questions