tags:

views:

518

answers:

3

The following code works when running the NetBeans IDE.

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

+1  A: 

Did you specify the build path to your icons in the options of Netbean before exporting the JAR? On Eclipse it's done by adding a source folder inside the Java Build Path as shown in this screenshot. Should be the same way on Netbeans?

Amokrane
+2  A: 

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)

Pindatjuh
@Pindatjuh, I've actually moved to a folder as you suggested, but the code, this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage("PlagiaLyzerIcon.png"));seems to work only in project root directory..
Mr CooL
+1  A: 

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")
Hector
even if I set the path for the folder, again,it works in project but not with Jar file.and, I'm not sure what's the problem with the code you gave? Constructor problem?
Mr CooL
I mean this code doesn't work,this.getFrame().setIconImage(new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png")));
Mr CooL
ooo now i see the problem...this.getFrame().setIconImage( new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png")).getImage() );should be work
Hector