我在jar中尝试加载图像,但是getResourceAsStream()始终返回null。

目录结构:

com/thesimpzone/watch_pugs/watch_pugs/{all my class files}
META-INF/MANIFEST.MF
resources/generic/mouse.png


Content.java:

public abstract class Content {

    protected Map<String, BufferedImage> images = new HashMap<String, BufferedImage>();

    protected String prefix;

    public Content(String prefix){
        this.prefix = prefix;
    }

    protected void loadImage(String name){
        System.out.println(name);
        System.out.println(prefix);
        String path = (prefix + name);
        System.out.println(path);
        String identifier = name.substring(0, name.lastIndexOf("."));
        try{
            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
            images.put(identifier, ImageIO.read(in));
        }catch(IOException | ClassCastException e){
            throw new RuntimeException("Image " + identifier + " at " + path + " could not be loaded.");
        }
    }
    [...]
}


GenericContent.java:

public class GenericContent extends Content {

    public GenericContent(){
        super("resources/generic/");
        this.loadContent();
    }

    @Override
    public void loadContent() {
        loadImage("mouse.png");
    }

}


堆栈跟踪:

mouse.png
resources/generic/
resources/generic/mouse.png
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1348)
    at com.thesimpzone.watch_pugs.watch_pugs.content.Content.loadImage(Content.java:29)
    at com.thesimpzone.watch_pugs.watch_pugs.content.GenericContent.loadContent(GenericContent.java:17)
    at com.thesimpzone.watch_pugs.watch_pugs.content.GenericContent.<init>(GenericContent.java:12)
    at com.thesimpzone.watch_pugs.watch_pugs.Canvas.<init>(Canvas.java:45)
    at com.thesimpzone.watch_pugs.watch_pugs.Framework.<init>(Framework.java:75)
    at com.thesimpzone.watch_pugs.watch_pugs.Window.<init>(Window.java:50)
    at com.thesimpzone.watch_pugs.watch_pugs.Window.<init>(Window.java:26)
    at com.thesimpzone.watch_pugs.watch_pugs.Window$1.run(Window.java:60)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


我不知道为什么类加载器找不到图像。我已经在编译的jar中查看了文件,然后在画图中打开良好,因此文件正常。我尝试了各种ClassLoader变体,包括getSystemClassLoader(),getClassLoader()和Content.class.getClassLoader();以及getResourceAsStream(path)而不是getResource(path).openStream()。我已经尝试过在前缀上加上或不带前缀“ /”,所以我全都没主意,而Google也无济于事。此外,似乎我在定义“前缀”时所做的工作很尴尬,因此,如果有更好的方法,有人向我展示了如何做,我将很高兴。

谢谢。

最佳答案

调用getResourceAsStream()时,这都是相对包和绝对包的关系,您要查找与Content包所在的根相关的东西。

类路径上没有“目录”,尤其是在.jar文件中,只有包

最好的使用方法是Thread.currentThread().getContextClassloader().getResourceAsStream(),带有完全合格的软件包,没有前导/

最好的原因是因为在应用程序容器内部,它们通常具有多个类加载器,因此您不必担心从哪个资源加载资源。

在您的情况下:

Thread.currentThread().getContextClassloader().getResourceAsStream("resources/generic/mouse.png");

如果使用此方法仍然出错,则您的.jar不会像您想象的那样构建,或者如果您是从IDE内部获取的,则可能不是将内容resource/generic/复制到类路径中。

我个人总是使用以下形式:

Thread.currentThread().getContextClassLoader().getResourceAsStream("path/to/resource/file.ext");总是可以从您调用它的地方以及您所处的类加载器中运行,并且明确说明了它在何处查找所需内容。

关于java - 为什么getResourceAsStream返回null?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26327430/

10-10 02:26