Mr CooL Mr CooL - 2 months ago 18
Java Question

Set Icon Image in Jar file

The following code works fine when running on NetBeans.

this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage("PlagiaLyzerIcon.png"));


However, once it was built into Jar file, the icon was gone.

Anyone has idea what's the problem? I realized I've to put the icon image on the root directory, however, after compiling into JAR, the icon gone.

Thanks for any help ...

Hi everyone, the problem was solved with the following code,

this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("plagialyzer/resources/PlagiaLyzerIcon.png")));


It works once compiled into jar file.

Answer

Use

this.getFrame().setIconImage(
new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png"))
);

instead.

Note:

this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:

getResource("yourfolder/PlagiaLyzerIcon.png")