Java - loading file for ImageIO.write doesn't work in .jar

三世轮回 提交于 2019-12-02 19:24:11

问题


I'm making a game in Java and I want to save the randomly generated map on an image and then load it. My code work without a problem in Eclipse, but when I export it to a .jar/.exe file it has problem with making a file ("mapf"). Thank you for your answers.

private void makeMap(){

    combined = new BufferedImage(maxX*Game.TILESIZE, maxY*Game.TILESIZE+16, BufferedImage.TYPE_INT_ARGB);

    //draw tiles
    Graphics g2 = combined.getGraphics();

    for(int y = 0; y < tiles[1].length; y++){
        for(int x = 0; x < tiles.length; x++){
            if(tiles[x][y] != 0){
                getTile(tiles[x][y]).render(g2, x * Game.TILESIZE, y * Game.TILESIZE);
            }
        }
    }

    //Save as new image     
    try {
        File mapf = new File("res/map.png");  //Here's a problem!!
        ImageIO.write(combined, "PNG", mapf);
    } catch (IOException e) {
        e.printStackTrace();
    }

    ImageLoader loader = new ImageLoader();
    Game.map = loader.load("/map.png");
}

Stack trace:

java.io.FileNotFoundException: res\map.png ([translated] System cannot find path)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at com.sedlacek.dor.level.Level.makeMap(Level.java:237)

回答1:


You seem to think you could treat the jar as a directory structure, thats not exactly true. You should not even think of writing to the jar file your code is running from (its possible, but involves many pitfalls).

Assuming your directory structure looks something like this:

MyProgram
   MyProgram.jar

and MyProgram is the working directory, you see there is no res directory. The "res" directory you created in Eclipse in the source tree is inside the jar. You cannot access it using the File API, and you will certainly not be able to write anything to it.

I'm not clear with the code shown what the purpose behind it is; if you are creating the file each time the program is run, and use it in only that run, I would not save it at all. Just return the BuffereImage you created an use it directly where you need it.

If the idea is to create it on the first run and simply load it on subsequent runs, you need to specifiy a location you can be sure exists and is writeable with your programs privileges. You could attempt to write it directly into the program folder (or create another "res" folder there), but that may not be writeable depending on under which user your program runs. You would normally set up proper structures and permissions during program installation.



来源:https://stackoverflow.com/questions/28483661/java-loading-file-for-imageio-write-doesnt-work-in-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!