简体   繁体   中英

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.

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.

geIO.write(image,“ png”,new File(System.getProperty(“ user.dir”))不在jar内,它是本地文件系统路径

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM