Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Icon Image in Jar file

Tags:

java

jar

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.

like image 945
Mr CooL Avatar asked Mar 20 '10 13:03

Mr CooL


3 Answers

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")
like image 66
hecvd Avatar answered Oct 17 '22 04:10

hecvd


That is because the Netbeans IDE has a different classpath, than when running the jar-file stand-alone (without Ant).

Assume your Netbeans project is at location /project/:

The classpath is: /project/build/classes/ and the project root /project/. If your icons are stored in: /project/myicons/, then they are part of the classpath, since /project/ is too. But when you build your project, only files in /project/build/classes/ will eventually end up in the jar-file, these files are "build" from /projcet/src/.

Solution:

Move your icons into a source-package: /project/src/myicons/

Or, add the /project/myicons/ folder to your sources (right-click your project -> Properties -> Sources -> add your folder there)

like image 3
Pindatjuh Avatar answered Oct 17 '22 03:10

Pindatjuh


Thanks a lot...

Here the code that works for me!!!!

    BufferedImage image = null;

    try {
        image = ImageIO.read(getClass().getClassLoader().getResource("images/frame.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }


    super.setIconImage(image);
like image 1
Gerson Castellanos Avatar answered Oct 17 '22 03:10

Gerson Castellanos