Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing icon in Windows *.exe from open-source platform-independent Java code

First of all, this is not a duplicate of the very common question of making an EXE from Java classes. I do not need to do that.

To solve NetBeans RFE #64612 without manual steps I need a Java (6+) library which can take an existing Windows *.exe file and replace its icon with a substitute in a common format. The executable, which is generic and prebuilt (distributed in binary form), already knows how to load an application-specific config file and then start the JRE with various application JARs etc.; the only problem is that it has a generic icon, and I would like to replace that icon as part of a pure Java build with an application-specific icon, so it looks prettier.

The library must be available under a nonviral open-source license; cross-platform (must run on Windows, Linux, Mac, Solaris) so cannot fork some OS-specific helper tool; and must accept PNG input, though the EXE must work on XP so according to Wikipedia should embed BMP format. At a high level, supposing Ant as a build tool, I would like something like this:

<replaceicon from="app.exe" to="hello.exe" icon="hello.png"/>

Does anyone know if a tool matching these specifications already exists? From various web searches I found Launch4J, but this appears to just fork windres for the real work, thus not trivially portable. I found JSmooth which looks more promising - appears to include Java code to handle the ICO codec and manipulate PE files - but it is GPL. WinRun4J looks to use native code for icon manipulation, though I had a hard time following its sources. Jimi supposedly handles the ICO format (for that matter the standard javax.imageio seems to as well) but I guess has no facility for updating PE resources.

like image 943
Jesse Glick Avatar asked Dec 28 '11 17:12

Jesse Glick


People also ask

How do I change the icon for an EXE file?

Now you'll see a shortcut to the EXE file on your desktop. Right-click the shortcut and select Properties. This opens a dialog window containing several tabs, including one called Shortcut. Click the Change Icon…


2 Answers

According to my Eclipse Rich Client Platform product builder,

  • Linux requires an XPM icon
  • MacOSX requires an ICNS file
  • Solaris requires 4 PM icons, Large, Medium, Small, and Tiny
  • Windows (32 bit) requires 6 separate BMP images, or an ICO file.

Your distribution package is going to have to contain all of these files to be platform independent.

I've not worked with the other platforms, but on Windows, you can change the program icon by right clicking on the existing icon and left clicking on Properties. Left click on the Shortcut tab, and left click on the Change Icon button. Browse over to the distribution directory, and select the ICO file.

I'm sure it's possible to automate the Windows icon change when you deliver the distribution package. I imagine it's possible on the other platforms.

like image 67
Gilbert Le Blanc Avatar answered Oct 05 '22 03:10

Gilbert Le Blanc


There's the PE/COFF 4J project which seems to be able to do what you want. It is licensed under the Common Public License (CPL).

Some notes on that:

  • The author seems to be the same as for WinRunJ. This project actually has a PE resource editor in it, called RCEDIT.exe, but it uses native windows calls as you point out yourself. Why the author didn't use his own project (PE/COFF 4J) to accomplish this beats me. It gives me some concern that perhaps the PE/COFF 4J project is abandoned.

  • The documentation page for PE/COFF 4J only mentions that the project is able to parse a PE file but as file as I can tell you can parse, then change something (e.g. an icon resource) and then write the image back to disk.

Like you I've also been searching for a pure Java solution that could manipulate resources in an .EXE (PE file) and have come up empty handed. This is the best bet so far.

Replacing an icon resource in an .EXE file is rather simple when using the native Win32 calls. When doing it from pure Java you have to make bloody sure that the PE file is consistent when you write it back to disk. I haven't scrutinized the PE file format in depth but I suppose that many references will change when replacing/adding a resource, not just the one related to the resource you're replacing/adding.

like image 41
peterh Avatar answered Oct 05 '22 01:10

peterh