blow
blow

Reputation: 13209

Java FileNotFoundException when running a jar file

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.

Upvotes: 0

Views: 4093

Answers (4)

Saunik singh
Saunik singh

Reputation: 21

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

You should use

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

Upvotes: 2

Bozho
Bozho

Reputation: 597076

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.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533500

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()

Upvotes: 0

AlexR
AlexR

Reputation: 115328

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.

Upvotes: 0

Related Questions