java - java - Why does this "new ImageIcon(getClass().getResource("files/icon.png")).getImage();" make my Program invisible? - answerstu - answerstu.com answerstu

java - Why does this "new ImageIcon(getClass().getResource("files/icon.png")).getImage();" make my Program invisible?

I wrote this

 Image icon = new ImageIcon(getClass().getResource("files/icon.png")).getImage();

in a JFrame-extending class to then set the icon of the frame with

this.setIconImage(icon);

I do it this way, intead of by simply using the Path, because otherwise the Icon would disappear, if I exported my Project as an executable .jar. (as seen here: Once exported, java cannot find/draw images)

The problem that occurs now is, that the executable .jar works the way it should (It creates a folder, as well as a .xml-file and writes into it), but without generating a visible frame. If I run the program in eclipse however, everything works fine and I see the frame. I found out, that the problem has to be in the creation of an ImageIcon, because if I run

getClass().getResource("files/icon.png");

everything works fine. The file is at it's place, too, so this shouldn't be the mistake.

Hope you can help me and thanks in advance.

1 Answer

  1. Lee- Reply

    2019-11-14

    Okay guys, it was all my fault... @JBNizet you had the right clue again... it was the case of the path "files/icon.png" -> "Files/icon.png"

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>