.jar将找不到内部资源图像,但编译后的程序可以找到。

huangapple go评论62阅读模式
英文:

.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:

.jar将找不到内部资源图像,但编译后的程序可以找到。

The compilation output layout looks like this:

.jar将找不到内部资源图像,但编译后的程序可以找到。

The .jar layout looks like this:

.jar将找不到内部资源图像,但编译后的程序可以找到。

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.

.jar将找不到内部资源图像,但编译后的程序可以找到。

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:

.jar将找不到内部资源图像,但编译后的程序可以找到。

The compilation output layout looks like this:

.jar将找不到内部资源图像,但编译后的程序可以找到。

The .jar layout looks like this:

.jar将找不到内部资源图像,但编译后的程序可以找到。

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.

.jar将找不到内部资源图像,但编译后的程序可以找到。

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

huangapple
  • 本文由 发表于 2020年4月5日 05:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61034941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定