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

查看:15
本文介绍了加载 .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 类中使用它.

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

使用

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>

所以我收集的是图像文件位置没有被正确读取,或者我以某种方式输入错误,返回空值并且没有图像正在加载.当 .Jar 运行时,面板出现,但没有绘制任何内容,并给出该错误.这个程序在 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 的文件夹中.例如我有文件夹:---文件.Jar---图像.png---图像文件夹-------更多图片在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 内部的图像?或者我是否必须像上面一样将图像打包在 jar 旁边的文件夹中,它可以工作,但我更喜欢寻找一个可运行的 .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天全站免登陆