【问题标题】:Java project working on Eclipse but not working when exported to runnable jar fileJava 项目在 Eclipse 上工作,但在导出到可运行的 jar 文件时不工作
【发布时间】:2015-04-11 17:09:59
【问题描述】:

我在将我的 java 文件导出到可运行的 jar 文件时遇到问题。 在eclipse中它工作正常,但是当我导出它时,它就不起作用了。

我已经尝试过 (java -jar MyJar.jar) 来获取日志,但它说 “无法访问 jar 文件”

我认为问题是因为这个:

java.net.URL logoOneUrl = getClass().getResource("/logo.png"); //already tried without the "/" and it doesn't work withouth the "/"
Icon logoOne = new ImageIcon(logoOneUrl)

因为当我将它放在评论 // 中时,当我导出它时它运行但没有图像。

我也尝试过这种方式: java.net.URL logoScience = getClassLoader().getResource("logo.png"); 但它也不起作用。 我究竟做错了什么? 如何在 JLabel 上导出带有图像的可运行 jar 文件?

(这是我的 JLabels 上的内容) JLabel lblImgLogin = new JLabel(logoOne);

更新

ImageIcon icon = createImageIcon("/logo.png","a pretty but meaningless splat");

protected ImageIcon createImageIcon(String path,String description) { java.net.URL imgURL = getClass().getResource(path); if (imgURL != null) { return new ImageIcon(imgURL, description); } else { System.err.println("Couldn't find file: " + path); } return null; }

JLabel lblImgLogin = new JLabel(icon);

同样的问题,在 eclipse 上工作,但导出到可运行的 jar 文件时不工作

【问题讨论】:

  • 文件在你的项目中的什么位置?
  • “无法访问 jar 文件”与代码没有太大关系。这与当前目录中不存在 jar 文件 MyJar.jar 的事实有关。
  • @JBNizet 但是如果他把图像取出来,为什么它会起作用?
  • @LiamdeHaas 我在 java 文件和 src 文件中的资源上得到了图像
  • 用可以读取zip文件的软件打开jar文件能看到图片吗?即 WinRAR、7zip 或解压缩。

标签: java eclipse jar jlabel


【解决方案1】:

这个是对的:

public class MainApp {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Testing");
        ImageIcon icon = createImageIcon("/resources/logo.png");
        JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER);

        //Set the position of the text, relative to the icon:
        label1.setVerticalTextPosition(JLabel.BOTTOM);
        label1.setHorizontalTextPosition(JLabel.CENTER);

        frame.getContentPane().add(label1);
        frame.pack();
        frame.setVisible(true);
    }

    protected static ImageIcon createImageIcon(String path) {
        URL imgURL = MainApp.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

【讨论】:

  • 第一个问题是,在图片前保留一个斜线。使它成为“/images/logo.png”而不是“images/logo.png”。其次,最好使用“资源”文件夹中的图片。
  • @SaqibRezwan 您回答和评论的内容根本不起作用。
  • @SaqibRezwan:可能是getResource()方法的一个小问题。但如果图片位于资源文件夹中,它运行完美。如果您制作自己的文件夹,如“图像”,它会中断。 ?但是您的代码也可以运行。
  • 那是我试图在我的回答中说出来的。
【解决方案2】:

创建一个文件夹名称“资源”(正是这个名称)。现在将 logo.png 保存在 Resources 文件夹中。现在使用如下代码。

ImageIcon imageIcon = new ImageIcon(getClass().getResource("/Resources/logo.png"))

编译并构建 jar(使用“生成的 jar 包所需的库”用于 Eclipse 导出)。如果在提取 jar 后,您会得到 Resources 文件夹和它下面的文件,那么希望它会起作用。对不起我的英语不好

【讨论】:

  • javax.swing.ImageIcon 处的 java.lang.NullPointerException。(未知来源)无法编译/运行
  • 路径不正确。您在哪里创建了 Resources 文件夹?它只是在项目路径中吗?如果路径正确,它应该可以工作。我遇到了这个问题并应用了上述程序。能否请您给出 png 文件的项目结构?
  • 您在代码中的资源之前是否使用了斜杠('/')?我犯了这个错误。你能给你的代码图片吗?还有来自 Eclipse 的项目结构?
  • 问题现在已经解决了,但是没关系... - 不,我没有在 Resources `ImageIcon imageIcon = new ImageIcon(getClass().getResource("Resources/logo.png "));`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多