【问题标题】:Jar file with packaged images exported from Eclipse not running [duplicate]带有从 Eclipse 导出的打包图像的 Jar 文件未运行 [重复]
【发布时间】:2016-08-06 21:06:43
【问题描述】:

我从 Eclipse IDE 导出了一个可运行的 jar 文件。在 Eclipse 中,该程序运行良好,但在导出程序时拒绝打开。为了澄清,图像位于源文件夹中,并通过资源加载器类通过 URL 加载。我使用 JD-GUI 来显示我的 jar 文件的内容,并将图像正确打包到文件中(包括图片)。为什么jar文件不能正常运行? The problem

这是我的 URL 加载器代码

public class MainPanel extends JPanel{
  BufferedImage img1, img2;
  URL url1 = ResourceLoader.class.getResource("1.jpg");
  URL url2 = ResourceLoader.class.getResource("2.jpg");
  MainPanel(){
    try {
        img1 = ImageIO.read(url1);
        img2 = ImageIO.read(url2);
    } catch (IOException e) {
        e.printStackTrace();
    }
}



public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(img1, 1, 0, null);
    g.drawImage(img2, img1.getWidth(), 0, null);
    if(g instanceof Graphics2D){
        Graphics2D g2d = (Graphics2D)g;
    }
  }
}

这里是资源加载器类

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

编辑::// 这是正在发生的错误的命令提示符读出 enter image description here

【问题讨论】:

  • “拒绝打开”?没有错误信息?在命令行中使用java -jar testFile2.jar 打开程序会看到什么?
  • 有什么问题?编译和运行应用程序时会发生什么?你有任何错误吗?如果是这样,请发布它们。
  • 另外,请确保您的 jpg 文件扩展名是小写的,就像您在字符串中写的一样。
  • @Code-Apprentice 我编辑了帖子,以便在命令提示符下运行的程序的错误显示在图像中。
  • 您应该直接将错误消息复制并粘贴到您的问题中。我建议查看问题顶部给出的链接。如果这不能帮助您解决问题,请编辑您的问题以解释原因。附带说明一下,在ResourceLoader 中使用static 方法可能不合适。更面向对象的设计会使它成为一种非静态方法。

标签: java eclipse jar


【解决方案1】:

在导出 jar 时,在“启动配置”中选择包含 main 方法的类。它应该可以正常工作。如果您仍然遇到问题,请使用“java -jar jarfilename”命令从命令行运行 jar 以查看确切的错误消息。

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2012-10-04
    • 1970-01-01
    相关资源
    最近更新 更多