加载.Jar文件或类路径中包含的资源(图像) [英] Loading resources (images) contained in a .Jar file or in the classpath

查看:85
本文介绍了加载.Jar文件或类路径中包含的资源(图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在堆栈交换上尝试了各种解决这个问题的各种解决方案,大多数人说要使用 getResourceAsStream()方法,我已经完成了。
这是Jar的资源输入方法。

So I've tried various reading various fixes for this problem on stack exchange most say to use getResourceAsStream() method, which I have done. This is my Resource input method for the Jar .

import java.io.InputStream;

    public class ResourceLoader {
        public static InputStream load(String path){
    InputStream input = ResourceLoader.class.getResourceAsStream(path);
    if(input == null){
        input = ResourceLoader.class.getResourceAsStream("/" + path);
    }
    return input;
    }
}

然后在我的 ImageLoader class。

This is then used in my ImageLoader class.

public class ImageLoader {

public BufferedImage load(String path){
    try {
//          return ImageIO.read(getClass().getResource(path));
        return ImageIO.read(ResourceLoader.load(path));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
    }
}

并使用<在主程序中加载图像/ p>

and the images are loaded in the main program using

ImageLoader loader = new ImageLoader();
    spriteSheet = loader.load("/spritesheet.png");

现在在eclipse游戏运行并加载所有图像完全正常。
但是我想要做的是将它导出到Jar,我已经使用一些教程完成了,
成功地将它导出到包含我使用的图像的资源文件夹。但是当我尝试运行.jar文件时,cmd行中会弹出此错误。

Now in eclipse the game runs and loads all images perfectly fine. But what I want to do is export it to Jar, which I have done using some tutorials and have succeeded in exporting it with the resource folder which contains my images that are used. But when I try and run the .jar file this error pops up in the cmd line.

Exception in thread "Thread-2" java,lang.IllegalArgumentException: input == null
!
    at javax.imageio.ImageIO.read<Image.IO.java:1348>
    at gfx.ImageLoader.load<ImageLoader.java:15>
    at man.Gaim.init(Game.java:100>
    at main.Game.run<Game.java:150>
    at java.lang.Thread.run<Thread.java:722>

所以我收集的是图像文件位置未正确读取或我在某种程度上错误地输入了它,它返回null并且没有任何图像正在加载。当运行.Jar时,Panel出现但没有任何颜色被绘制并且给出了错误。
这个程序在eclipse中完美地工作没有错误和所有图片加载。

So what I'm gathering is that the image file locations are not being read properly or I inputed them wrong somehow which is returning null and none of the images are loading. When the .Jar is run the Panel appears but nothing is painted to it and that error is given. This program does work perfectly in eclipse with no errors and all images loading.

编辑1:
Robermann你的解决方案getClass()。getClassLoader()。getResourceAsStream(path))工作。唯一的问题是我需要将图像文件放在带有jar的文件夹中。
例如我有
文件夹:
--- File.Jar
--- Images.png
--- ImageFolder
---- ---更多imgaes in imagefolder.png

EDIT 1: Robermann your solution for the getClass().getClassLoader().getResourceAsStream(path)) works. The only thing is I need to have the image files in a folder with the jar. For instance I have Folder: ---File.Jar ---Images.png ---ImageFolder -------More imgaes in imagefolder.png

我可以加载所有图像。我的实际问题是,当我导出.Jar时,图像也位于内部,是否可以使用位于.jar内的图像?或者我必须将imga包装在罐子旁边的文件夹中,如上所述,它可以工作,但我更需要一个可运行的.Jar,我可以转发给托架而不需要它们也需要.jar之外的图像。

I can load all the images when they are located like that. My actual question was when i export a .Jar the Images are also located inside is it possible to just use the images that are located inside the .jar? Or do I have to pack the imgaes in a folder alongside the jar as above, It works but i was more looking for a runnable .Jar that i could just transer to tohers without having them also need the images outside the .jar.

推荐答案

如何加载类路径资源的问题非常反复出现,对Java新手来说有点混乱:一些答案建议 class.getClassLoader()。getResourceAsStream ,其他 class.getResourceAsStream ,尽管它们的语义略有不同:

The question of how to load classpath resources is quite recurring, and a bit confusing for a Java newbie: some answers suggest class.getClassLoader().getResourceAsStream, others class.getResourceAsStream, although they have a slight different semantic:


  1. class.getResourceAsStream 执行路径转换

  2. class.getClassLoader()。getResourceAsStream 不转换路径
  1. class.getResourceAsStream does a path translation
  2. class.getClassLoader().getResourceAsStream does not translate the path

为了更好地展示差异,我将提出以下测试类,它以4种不同的方式尝试加载相同的资源(图像),只有2个工作取决于使用的路径。 Jar内容树是:

For better show the difference, I'm going to propose the following test class, which in 4 different ways try to load the same resource (an image), only 2 working depending on the used path. The Jar content-tree is:

班级:

package image;

import java.io.InputStream;

public class ImageLoader {
    public static void main(String[] args ){
        String cmd = null;
        InputStream is = null;
        final String image = "save.png";

        if("test1".equals(args[0])){
            cmd = "ImageLoader.class.getClassLoader().getResourceAsStream(\""+image+"\")";
            is = ImageLoader.class.getClassLoader().getResourceAsStream(image);     //YES, FOUND


        }else if("test2".equals(args[0])){
            cmd = "ImageLoader.class.getResourceAsStream(\""+image+"\")";
            is = ImageLoader.class.getResourceAsStream(image);                      //NOT FOUND

        }else if("test3".equals(args[0])){
            cmd = "ImageLoader.class.getResourceAsStream(\"/"+image+"\")";
            is = ImageLoader.class.getResourceAsStream("/"+image);                  //YES, FOUND

        }else if("test4".equals(args[0])){
            cmd = "ImageLoader.class.getClassLoader().getResourceAsStream(\"/"+image+"\")";
            is = ImageLoader.class.getClassLoader().getResourceAsStream("/"+image); //NOT FOUND

        }else {
            cmd = " ? ";
        }

        System.out.println("With "+cmd+", stream loaded: "+(is != null));
    }
}

运行时间:


java -cp resLoader.jar image.ImageLoader test4

java -cp resLoader.jar image.ImageLoader test4

希望这个课程可以帮助理解不同的行为。

Hope this class can help in understanding the different behaviour.

这篇关于加载.Jar文件或类路径中包含的资源(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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