Java - ImageIO.write 的加载文件在 .jar 中不起作用 [英] Java - loading file for ImageIO.write doesn't work in .jar

查看:22
本文介绍了Java - ImageIO.write 的加载文件在 .jar 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 制作游戏,我想将随机生成的地图保存在图像上,然后加载它.我的代码在 Eclipse 中没有问题,但是当我将它导出到 .jar/.exe 文件时,它在制作文件(mapf")时出现问题.感谢您的回答.

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");
}

堆栈跟踪:

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)

推荐答案

您似乎认为可以将 jar 视为目录结构,但事实并非如此.您甚至不应该考虑写入运行代码的 jar 文件(这是可能的,但涉及许多陷阱).

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

而 MyProgram 是工作目录,您会看到没有 res 目录.您在 Eclipse 中的源代码树中创建的res"目录位于 jar .您无法使用 File API 访问它,当然也无法向其中写入任何内容.

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.

我不清楚代码显示其背后的目的是什么;如果您每次运行程序时都创建文件,并且仅在该运行中使用它,我根本不会保存它.只需返回您创建的 BuffereImage 并直接在您需要的地方使用它.

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.

如果您的想法是在第一次运行时创建它并在后续运行中简单地加载它,您需要指定一个可以确定存在并且可以使用您的程序权限写入的位置.您可以尝试将其直接写入程序文件夹(或在那里创建另一个res"文件夹),但 可能无法写入,具体取决于您的程序在哪个用户下运行.您通常会在程序安装过程中设置适当的结构和权限.

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.

这篇关于Java - ImageIO.write 的加载文件在 .jar 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆