简体   繁体   English

Java加载ImageIO.write的文件在.jar中不起作用

[英]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. 我正在用Java开发游戏,我想将随机生成的地图保存在图像上,然后加载它。 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"). 我的代码在Eclipse中工作正常,但是当我将其导出到.jar / .exe文件时,它在制作文件(“ 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. 您似乎认为您可以将jar视为目录结构,事实并非如此。 You should not even think of writing to the jar file your code is running from (its possible, but involves many pitfalls). 您甚至不应该考虑将代码从中运行到jar文件中(可能的话,但是会涉及很多陷阱)。

Assuming your directory structure looks something like this: 假设您的目录结构如下所示:

MyProgram
   MyProgram.jar

and MyProgram is the working directory, you see there is no res directory. MyProgram是工作目录,您将看到没有res目录。 The "res" directory you created in Eclipse in the source tree is inside the jar. 您在Eclipse的源代码树创建的“资源”目录是罐内 You cannot access it using the File API, and you will certainly not be able to write anything to it. 您无法使用File API对其进行访问,并且您肯定无法向其中写入任何内容。

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. 只需返回您创建的BuffereImage即可在需要的地方直接使用它。

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. 您可以尝试将其直接写入程序文件夹(或在此处创建另一个“ res”文件夹),但这可能无法写入,具体取决于程序在哪个用户下运行。 You would normally set up proper structures and permissions during program installation. 通常,您将在程序安装过程中设置适当的结构和权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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