简体   繁体   English

Jar 文件执行错误 Java/Eclipse

[英]Jar file execution errors Java / Eclipse

I am having trouble with an exported jar for a game I made in Java.我在使用 Java 制作的游戏导出 jar 时遇到问题。 The game is due to be presented tomorrow and the jar will not execute.游戏将于明天推出,jar 将不会执行。

I am running command java -jar filename.jar我正在运行命令 java -jar filename.jar

Here is a part of the stack trace in the cmd prompt:这是 cmd 提示符中堆栈跟踪的一部分:

Image load failed: Resources/Images/shop/jet.png
javax.imageio.IIOException: Can't read input file!
        at javax.imageio.ImageIO.read(Unknown Source)
        at ImageLoader.loadImage(ImageLoader.java:21)
        at ImageLoader.loadScaledImage(ImageLoader.java:34)
        at ImgButton.<init>(ImgButton.java:18)
        at Carnies.createButtons(Carnies.java:343)
        at Carnies.simpleInitialize(Carnies.java:82)
        at Frame.<init>(Frame.java:90)
        at Carnies.<init>(Carnies.java:64)
        at CarnieRunner.makeGUI(CarnieRunner.java:62)
        at CarnieRunner.<init>(CarnieRunner.java:27)
        at CarnieRunner.main(CarnieRunner.java:131)
Exception in thread "main" java.lang.ExceptionInInitializerError
        at Carnies.createButtons(Carnies.java:361)
        at Carnies.simpleInitialize(Carnies.java:82)
        at Frame.<init>(Frame.java:90)
        at Carnies.<init>(Carnies.java:64)
        at CarnieRunner.makeGUI(CarnieRunner.java:62)
        at CarnieRunner.<init>(CarnieRunner.java:27)
        at CarnieRunner.main(CarnieRunner.java:131)
Caused by: java.lang.NullPointerException
        at GifHandler.<init>(GifHandler.java:15)
        at GifHandler.<clinit>(GifHandler.java:7)
        ... 7 more

It appears the problem is that my images aren't loading.看来问题是我的图像没有加载。 Here is the line in the program that handles the image loading:这是程序中处理图像加载的行:

public static BufferedImage loadImage(String fileName) {
        try { 
            File file;
            file = new File(fileName);           
            BufferedImage sub = ImageIO.read(file); 
            return toCompatibleImage(sub);
        }
        catch (IOException e) { 
            System.out.println("Image load failed: " +fileName);
            e.printStackTrace();
            return null;
        }
    }

This all works beautifully in eclipse but no luck on the Jar.这一切在 eclipse 中都很好用,但在 Jar 上没有运气。 Any advice would be wonderful.任何建议都会很棒。 If having the entirety of my project would help troubleshoot it's located publically at github.com/madamsmall/carnie Thank you!如果拥有我的整个项目有助于排除故障,它位于 github.com/madamsmall/carnie 上,谢谢!

jar 文件中的 Image 不再是File ,您需要更改代码以将其作为类路径中的资源读取

Could you please look inside of the .jar file and see if this location exist "Resources/Images/shop/" and if the file is being packaged correctly?您能否查看 .jar 文件的内部并查看此位置是否存在“Resources/Images/shop/”以及文件是否正确打包?

The only time I have seen this behavior is when the package instruction on a POM file (Maven) is incorrect.我唯一一次看到这种行为是在 POM 文件 (Maven) 上的包指令不正确时。

Thanks.谢谢。

My bad, totally miss that, that answer is correct is a resource.我的错,完全错过了,那个答案是正确的,这是一种资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM