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

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

问题描述

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

  private void makeMap(){

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

//绘制瓷砖
图形g2 = combined.getGraphics(); (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);
}
}
}

//另存为新图像
try {
File mapf = new File(res / map。 PNG); //这是一个问题!
ImageIO.write(合并,PNG,mapf);
} catch(IOException e){
e.printStackTrace();
}

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

堆栈跟踪:

  java.io.FileNotFoundException:res\map.png([翻译]系统找不到路径)
在java.io.RandomAccessFile.open(Native Method)
在java.io.RandomAccessFile。< init>(未知源)
在javax.imageio.stream.FileImageOutputStream。< init>(未知来源)
在com.sun.imageio.spi .FileImageOutputStreamSpi.createOutputStreamInstance(未知源)
在javax.imageio.ImageIO.createImageOutputStream(未知源)
在javax.imageio.ImageIO.write(未知源)
在com.sedlacek.dor .level.Level.makeMap(Level.java:237)


解决方案

你似乎认为你可以将jar视为一个目录结构,这并不完全正确。你甚至不应该将写入您的代码正在运行的jar文件(可能的,但涉及许多陷阱)。



假设您的目录结构如下所示:

  MyProgram 
MyProgram.jar

和MyProgram是工作目录,你看到没有 res 目录。在源树中在Eclipse中创建的res目录是内的。您不能使用File API访问它,您一定无法写入任何内容。



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



如果想要在第一个运行时创建它在后续运行时加载它,您需要指定您可以确定存在的位置,并且可以使用程序权限进行写入。您可以尝试将其直接写入程序文件夹(或在其中创建另一个res文件夹),但是根据您的程序运行的用户,可能无法写入。通常在程序安装过程中设置正确的结构和权限。


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.

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

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