space1188
space1188

Reputation: 1

Read a file within a JAR

Now when I do this in eclipse it works just fine how I want it to, but when I export it into a jar file and run it, I get an error. I think this is because getting the file from a jar file is different.

Error I'm having

C:\Users\computer\res\levels\savelevel.png 
(The system cannot find the path specified)

My code

ImageIO.write(image, "png", 
 new File(System.getProperty("user.dir") + "/res/levels/savelevel.png"));

What I am trying to do here is I am trying to write to an image using another image.

Upvotes: 0

Views: 65

Answers (2)

Dici
Dici

Reputation: 25950

You should be using something like getClass().getResource("a/path/relative/to/the/package/of/your/loading/class") when you want to read something from a JAR. Now if you want to write, I can't remember if it's possible or not to mutate a file in the jar you are currently running in, but in any case I would strongly advise you not to do so.

As Srikanth pointed out, the jar must be available in your classpath, if that needs to be mentioned.

Upvotes: 1

bichito
bichito

Reputation: 1426

geIO.write(image, "png", new File(System.getProperty("user.dir") Does not look inside the jar. It is a local filesystem path

Upvotes: 0

Related Questions