Skip to content

[native-image] ClassCastException when using java.awt.TrayIcon #6659

@n1kub

Description

@n1kub

Describe the issue
Getting ClassCastException when using a SystemTray icon.

Steps to reproduce the issue
Sample code:

package org.example;

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Application {

    public static void main(String[] args) {
        String folderPath = System.getProperty("user.home");
        createTrayMenu(folderPath);
    }

    public static void createTrayMenu(String applicationFolderPath) {
        if (SystemTray.isSupported()) {
            // Create a popup menu
            PopupMenu popup = new PopupMenu();

            MenuItem openLogsFolder = new MenuItem("Open log folder");
            openLogsFolder.addActionListener(new ActionListener() {
                                                 @Override
                                                 public void actionPerformed(ActionEvent e) {
                                                     Desktop desktop = Desktop.getDesktop();
                                                     try {
                                                         File dirToOpen = new File(applicationFolderPath);
                                                         desktop.open(dirToOpen);
                                                     } catch (IOException ex) {
                                                         try {
                                                             Files.createDirectories(Path.of(applicationFolderPath));
                                                         } catch (IOException IOe) {
                                                             IOe.printStackTrace();
                                                             System.out.println("Unknown file error");
                                                         }
                                                     }
                                                 }
                                             }

            );
            popup.add(openLogsFolder);

            MenuItem exitItem = new MenuItem("Exit");
            exitItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            popup.add(exitItem);
            Image image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
            final TrayIcon trayIcon = new TrayIcon(image, "Sample", popup);
            trayIcon.setImageAutoSize(true);
            trayIcon.setToolTip("Sample");

            try {

                SystemTray.getSystemTray().add(trayIcon);
            } catch (AWTException e) {
                System.err.println("TrayIcon could not be added.");
            } catch (ClassCastException ignored) {
            }
        } else {
            System.err.println("System tray not supported!");
        }

    }

}

Describe GraalVM and your environment:

  • GraalVM version: 22.3.1
  • JDK major version: 17
  • OS: Windows 10
  • Architecture: AMD64

More details
Exception:

Exception in thread "AWT-Windows": java.lang.ClassCastException
java.lang.ClassCastException: java.awt.TrayIcon cannot be cast to java.awt.Component
        at [email protected]/sun.awt.windows.WToolkit.eventLoop(WToolkit.java)
        at [email protected]/sun.awt.windows.WToolkit.run(WToolkit.java:365)
        at [email protected]/java.lang.Thread.run(Thread.java:833)
        at com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
        at com.oracle.svm.core.windows.WindowsPlatformThreads.osThreadStartRoutine(WindowsPlatformThreads.java:178)

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions