How to change executable jar file icon?
Asked Answered
N

6

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?

Nameplate answered 6/3, 2012 at 19:56 Comment(4)
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 IconMornay
by default each GUI framework has its limitations against write code by your handsMornay
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.Litt
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.Proficient
D
10

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

launch4j website

Depolymerize answered 31/10, 2014 at 5:38 Comment(3)
"cross platform" as in: you can use it in your build process on any platform, but it creates a Windows-only executable, right?Synclastic
it doesn't change .jar file icon. It creates .exe file from .jar(and only then you can set icon for .exe)Printery
@Tomek They do now. Follow the link.Baku
E
7

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

Erelong answered 9/3, 2012 at 13:16 Comment(2)
+1 nice wrapper, but my app needs to work on non-windows hosts.Nameplate
Looks good, but like @Nameplate 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.Exaltation
A
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 Resource Hacker and then follow turtorials on how to change icon. Otherwise, you can't change .jar's icon

Argosy answered 9/5, 2017 at 14:57 Comment(1)
Plus One for Resource HackerPeruse
A
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.

Alnico answered 22/5, 2014 at 12:54 Comment(1)
See also this answer to ensure things don't fail on non-OS X machines. (And this needs the compilation to be done on a Mac.)Synclastic
J
0

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.

Jilleen answered 8/8, 2017 at 7:46 Comment(0)
B
0

This is kinda late but if you want to change the desktop icon and the overall icon when you open the .jar file, you can use the Launch4j and this.

ClassLoader classLoader = Main.class.getClassLoader();
URL resourceURL = classLoader.getResource("nameOfYourPackage/nameOfImage.png");
    
ImageIcon thisIsTheName = new ImageIcon(resourceURL);
    
frame.setIconImage(thisIsTheName);

This works with in audio too

Bikini answered 30/3 at 11:27 Comment(1)
I Somehow forgot to format my code snipped but here it isBikini

© 2022 - 2024 — McMap. All rights reserved.