NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.16k stars 14.19k forks source link

Java AWT TrayIcon doesn't work on GNOME: hard-coded `/usr/bin/gnome-shell` in UNIXTookit #356340

Open SamLukeYes opened 3 hours ago

SamLukeYes commented 3 hours ago

Describe the bug

Java programs using AWT TrayIcon cannot create tray icons on NixOS GNOME, due to the hard-coded /usr/bin/gnome-shell in private method UNIXTookit.getGnomeShellMajorVersion.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Enable AppIndicator extension on GNOME
  2. Make sure javac and java are in PATH, e.g. through nix-shell -p jdk
  3. Save the following code as SimpleTray.java
import java.awt.*;
import java.awt.image.*;

public class SimpleTray {
    public static void main(String[] args) throws AWTException {
        System.out.println(SystemTray.isSupported());
        TrayIcon trayIcon = new TrayIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB), "Simple Tray");
        SystemTray.getSystemTray().add(trayIcon);
    }
}
  1. javac SimpleTray.java && java SimpleTray

Expected behavior

The Java program should print "true" in stdout, and a tray icon should show up until Ctrl+C is pressed.

Screenshots

图片

Additional context

Tested on jdk11, jdk17 and jdk21, and this issue exists on all of them.

A workaround is to use an FHS env with /usr/bin/gnome-shell, e.g.

{ pkgs ? import <nixpkgs> {} }:

(pkgs.buildFHSUserEnv {
  name = "java-tray-test";
  targetPkgs = p: with p; [
    jdk
    gnome-shell
  ];
}).env

Metadata

Notify maintainers

@NixOS/java


Note for maintainers: Please tag this issue in your PR.


Add a :+1: reaction to issues you find important.

tomodachi94 commented 2 hours ago

Confirmed this behavior exists.

Upstream code: https://github.com/openjdk/jdk/blob/d2e4b51133674381f2e220abc0e07704e5346b05/src/java.desktop/unix/classes/sun/awt/UNIXToolkit.java#L277

I suspect upstream might want this path to be configurable at runtime build-time, but a fix in Nixpkgs would be a simple subtituteInPlace for the relevant file in postPatch.

SamLukeYes commented 2 hours ago

Confirmed this behavior exists.

Upstream code: https://github.com/openjdk/jdk/blob/d2e4b51133674381f2e220abc0e07704e5346b05/src/java.desktop/unix/classes/sun/awt/UNIXToolkit.java#L277

I suspect upstream might want this path to be configurable at runtime, but a fix in Nixpkgs would be a simple subtituteInPlace for the relevant file in postPatch.

Thanks for the link. I searched the upstream bug tracker just now, and found a related issue: https://bugs.openjdk.org/browse/JDK-8325914