24

I have been able to change the runtime icon using this example like this

getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage(getClass()
.getClassLoader().getResource("MyProject/resources/myIcon.png")));

but is there a way to tell NetBeans to use myIcon.png for the executable jar file (MyProject/dist/MyProject.jar) icon?

Community
  • 1
  • 1
jacknad
  • 13,483
  • 40
  • 124
  • 194
  • 1
    if your project is based on Java Desktop Aplication (JSR296), then not possible to change Java cup Icon directly, this method is private, non accesible from outside, sure is possible, but required another hacks to rest of container cca 300 use_less code lines just for JFrame's Icon – mKorbel Mar 06 '12 at 20:01
  • 1
    by default each GUI framework has its limitations against write code by your hands – mKorbel Mar 06 '12 at 20:35
  • Adobe has figured out how to set the icon for their files, so it is possible. I think that this issue is the single-most important reason that executable jars (without wrappers) have not caught on Windows. – Thorbjørn Ravn Andersen Jan 16 '17 at 01:54
  • It is so common need that Java should have given us a way to do it instead of us needing third-party software to do this. – Pramod Dec 06 '18 at 06:42

5 Answers5

10

A launch wrapper is the solution, I use launch4j, a cross platform lightweight wrapper

launch4j website

Gary Benade
  • 507
  • 4
  • 16
7

You may use JSmooth to create executable java file and also associate icon to it.

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
krupal
  • 403
  • 4
  • 15
  • 3
    +1 nice wrapper, but my app needs to work on non-windows hosts. – jacknad Mar 09 '12 at 15:35
  • 1
    Looks good, but like @jacknad said is only for Windows hosts (which is fine), but the big down for me is that it hasn't been in active development for a little more than 5 years. – Kingsolmn Aug 11 '12 at 15:18
3

No, there isn't, because icon is determined by Java. Only way to do so (at least for Windows) is to use wrapper like launch4j and then set icon path. If you already have wrapped exe with no or wrong icon, you can use Recource Hacker and then follow turtorials on how to change icon. Otherwise, you can't change .jar's icon

Marko Zajc
  • 307
  • 3
  • 14
1

A possibility on mac is to use this line:

com.apple.eawt.Application.getApplication().setDockIconImage( new ImageIcon(getClass().getResource( "resources/appIcon.png" )).getImage());

It set your icon for the dock. I can't test it for now, i'm not working on a Mac but try it and get me up do date.

SteamFire
  • 367
  • 2
  • 14
  • See also [this answer](http://stackoverflow.com/questions/11253772/setting-the-default-application-icon-image-in-java-swing-on-os-x/29652666#29652666) to ensure things don't fail on non-OS X machines. (And this needs the compilation to be done on a Mac.) – Arjan Jul 04 '15 at 18:22
1

As known, it is not possible to change .jar icon cause if you want to do it you should change all the .jar files in your operating system. Anyhow, a solution is possible. You can create a shortcut for the .jar and then you can change it: Right button >> Properties >> Change Icon.

enter image description here

p.s. The icon file should be a .ico, so you can use many site for conversion in .ico extension if your image has got another one.

Alternative 1: You can use also Launch4j, it's most simple way to generate application executable file.