【发布时间】: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方法可能不合适。更面向对象的设计会使它成为一种非静态方法。