Search code examples
javaubuntuiconsdeb

Ubuntu DEB installer makes all Java applications have the same icon


I've created a DEB installer of my Java application for Debian-based systems. In my DEB package I use a *.desktop file like this:

[Desktop Entry]
Encoding=UTF-8
Version=${version}
Type=Application
Terminal=false
Exec=java -jar /usr/lib/name-of-my-app/${jar}
Name=${name}
Icon=/usr/share/icons/hicolor/128x128/apps/name-of-my-app.png

Everything works correctly, my applications installs well and runs well. But I've noticed very strange thing: after the first run of my app on Ubuntu, all other processes named "java" in the whole system now have the icon, I've specified in the *.desktop file. Is this an Ubuntu bug? Is this a bug in my installer?


Solution

  • For those looking for a solution for their Java applications in Ubuntu and other Linuxes. There seems to be 2 ways to do this, depending on what UI toolkit you use.

    1. For those who use AWT and/or Swing (which in turn is based on AWT), you have only a workaround for now. http://elliotth.blogspot.com/2007/02/fixing-wmclass-for-your-java.html - this guy managed to fix the WM_CLASS:

      Toolkit xToolkit = Toolkit.getDefaultToolkit();
      java.lang.reflect.Field awtAppClassNameField =
        xToolkit.getClass().getDeclaredField("awtAppClassName");
      awtAppClassNameField.setAccessible(true);
      awtAppClassNameField.set(xToolkit, "MyAppName");
      
    2. For those who use SWT, there is a simpler, documented way:

      Display.setAppName("MyAppName");

    After you've done (1) or (2), you can now test this by running

    xprop|grep WM_CLASS
    

    It will change your cursor to be a cross sign. With the new cross-sign cursor click on the window of your running application and make sure that the output is

    WM_CLASS(STRING) = "MyAppName", "MyAppName"
    

    where "MyAppName" is the string you've passed to AWT/SWT earlier.

    If everything goes fine, then add a line to the MyAppName.desktop file like the following:

    StartupWMClass=MyAppName