英文:
.jar will not find internal resource images but compiled program does
问题
I've been trying to load some pictures to use in my Swing UI and while my compiled program will load the images correctly, my .jar does not find the images.
First of all, I've marked my resource file as "Resource Root". The project layout looks like this:
The compilation output layout looks like this:
The .jar layout looks like this:
Now, the code that's been loading the pictures is inside the gui package; here it is:
try {
System.out.println(this.getClass().getResource("../images/buttonClip.png").getPath());
attchmntBtnImg = ImageIO.read(getClass().getResource("../images/buttonClip.png"));
sendMsgBtnImg = ImageIO.read(getClass().getResource("../images/buttonForward.png"));
}
catch (IOException e) {
e.printStackTrace();
}
The print line output prints correctly the path of the picture
/C:/Users/path/to/Project/out/production/Peer2Party_desktop/images/buttonClip.png
While there are no errors when running it from IntelliJ IDEA, the artifact generated (if I run it from the cmd, with or without administrator privileges) gives me a NullPointerException at the System.out.println()
, but, undoubtedly, that's not the cause of the error, since it'll still crash on the next line even if I remove the println.
I've tried to do these in order to load the images, to no avail:
this.getClass().getResource("/../images/buttonClip.png");
getClass().getResource("/../images/buttonClip.png");
this.getClass().getResource("images/buttonClip.png");
getClass().getResource("images/buttonClip.png");
this.getClass().getResourceAsStream("images/buttonClip.png");
getClass().getResourceAsStream("images/buttonClip.png");
Any help is very VERY welcome.
Important note: I'm on Windows 10, the pictures are inside the correct path (file.jar/images/buttonClip.png), but at runtime the .class won't find it
英文:
I've been trying to load some pictures to use in my Swing UI and while my compiled program will load the images correctly, my .jar does not find the images.
First of all, I've marked my resource file as "Resource Root". The project layout looks like this:
The compilation output layout looks like this:
The .jar layout looks like this:
Now, the code that's been loading the pictures is inside the gui package; here it is:
try {
System.out.println(this.getClass().getResource("../images/buttonClip.png").getPath());
attchmntBtnImg = ImageIO.read(getClass().getResource("../images/buttonClip.png"));
sendMsgBtnImg = ImageIO.read(getClass().getResource("../images/buttonForward.png"));
}
catch (IOException e) {
e.printStackTrace();
}
The print line output prints correctly the path of the picture
/C:/Users/path/to/Project/out/production/Peer2Party_desktop/images/buttonClip.png
While there are no errors when running it from intellij idea, the artifact generated (if I run it from the cmd, with or without administrator priviledges) gives me a NullPointerException at the System.out.println(), but, undoubtedly, that's not the cause of the error, since it'll still crash on the next line even if I remove the println.
I've tried to do these in order to load the images, to no avail:
this.getClass().getResource("/../images/buttonClip.png");
getClass().getResource("/../images/buttonClip.png");
this.getClass().getResource("images/buttonClip.png");
getClass().getResource("images/buttonClip.png");
this.getClass().getResourceAsStream("images/buttonClip.png");
getClass().getResourceAsStream("images/buttonClip.png");
Any help is very VERY welcome.
Important note: I'm on windows 10, the pictures are inside the correct path(file.jar/images/buttonClip.png), but at runtime the .class won't find it
答案1
得分: 2
只需使用
this.getClass().getResource("/images/buttonclip.png");
它会从项目根目录查找文件。
英文:
Simply use
this.getClass().getResource("/images/buttonclip.png");
it looks for the file from project root directory
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论