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

社会主义新天地 提交于 2019-12-04 03:53:43

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.

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.

It looks like the eclipse project has written a small java application to replace the icons.

IconExe from the eclipse project

The app.exe and app64.exe in Netbeans seem to have the follow icons in the resource section:
48 x 48 32bit
32 x 31 32bit
48 x 48 8bit
31 x 31 8bit
16 x 16 8bit

I'm guessing the 32 x 31 is a mistake

You simply have to replace the first ICO or BMP in the executables resource section. That one is automatically picked by explorer as the icon to display.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!