Question

In my project I load my resource using

getClass().getResource("/package/my_reource.file").getFile()

All works good when I run the project in netbeans, but if I run the jar file, I get FileNotFoundException, why?

Thanks.

Was it helpful?

Solution

I don't think you need the filename. You rather need its content. So use getResourceAsStream() to obtain the InputStream and read the content from there.

OTHER TIPS

You can use InputStream rather than getClass().getResource("/package/my_reource.file").getFile()

You should use

getClass.getResourceAsStream("/package/myresource.file")

Check your jar. I believe that your file is not there. The reasons depend on how are you creating your jar. If you are doing it using netbeans, check your settings. Probably it includes only *.class files? The same is about ant. Check tag.

The getFile() returns the file path portion of the URL returned by getResource() So if its in the Jar, you have to read the jar to gett he file. If its on the filesystem you can read using FileInputStream.

If you want to get the InputStream and you don't create where you get it from use getResourceAsStream()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top