我有一个包含一些JSliders的小GUI,你有什么。我可以将它导出为.jar,它可以毫无问题地运行。然后我决定添加一些图像,并将它们放在eclipse中我的包中的子文件夹中。现在当我导出文件并尝试运行它时,我得到以下错误(在终端中运行时)
Henry-MAC:desktop 2010imac01$ java -jar trust.jar
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1362)
at trust.GUI.<init>(GUI.java:75)
at trust.main.main(main.java:11)
经过大量的谷歌搜索后,我认为这与我的清单(日食生成)中没有包含的.jar有关。如何在清单中查看/添加必要的类路径。
这是我打开图像的部分(我认为是错误)
try {
n = ImageIO.read((this.getClass().getResource("smiles/n.png")));
s5 = ImageIO.read((this.getClass().getResource("smiles/5.png")));
s4 = ImageIO.read((this.getClass().getResource("smiles/4.png")));
s3 = ImageIO.read((this.getClass().getResource("smiles/3.png")));
s2 = ImageIO.read((this.getClass().getResource("smiles/2.png")));
f5 = ImageIO.read((this.getClass().getResource("smiles/-5.png")));
f4 = ImageIO.read((this.getClass().getResource("smiles/-4.png")));
f3 = ImageIO.read((this.getClass().getResource("smiles/-3.png")));
f2 = ImageIO.read((this.getClass().getResource("smiles/-2.png")));
smile = new Easel(n);
} catch (IOException e) {
System.out.println("error");
}
答案 0 :(得分:1)
当图像打包在JAR中时,如果您使用的是以下内容,则类加载器可以找到您的图像:
this.getClass().getResource("/resources/icon.png")
OR
this.getClass().getResourceAsStream("/resources/icon.png")
在您的情况下,代码应如下所示:
try {
n = ImageIO.read((this.getClass().getResource("/smiles/n.png")));
s5 = ImageIO.read((this.getClass().getResource("/smiles/5.png")));
s4 = ImageIO.read((this.getClass().getResource("/smiles/4.png")));
s3 = ImageIO.read((this.getClass().getResource("/smiles/3.png")));
s2 = ImageIO.read((this.getClass().getResource("/smiles/2.png")));
f5 = ImageIO.read((this.getClass().getResource("/smiles/-5.png")));
f4 = ImageIO.read((this.getClass().getResource("/smiles/-4.png")));
f3 = ImageIO.read((this.getClass().getResource("/smiles/-3.png")));
f2 = ImageIO.read((this.getClass().getResource("/smiles/-2.png")));
smile = new Easel(n);
} catch (IOException e) {
System.out.println("error");
}
OR
try {
n = ImageIO.read((this.getClass().getResourceAsStream("/smiles/n.png")));
s5 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/5.png")));
s4 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/4.png")));
s3 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/3.png")));
s2 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/2.png")));
f5 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/-5.png")));
f4 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/-4.png")));
f3 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/-3.png")));
f2 = ImageIO.read((this.getClass().getResourceAsStream("/smiles/-2.png")));
smile = new Easel(n);
} catch (IOException e) {
System.out.println("error");
}
希望这符合您的目的......
谢谢!
答案 1 :(得分:-1)
试试这个:
BufferedImage n = ImageIO.read(new File("./smiles/n.png"));
我认为图片 n.png 位于项目文件夹下的smiles文件夹中。