简体   繁体   中英

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

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)

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.

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