Skip to content

[native-image] ClassCastException when using SystemTray #3414

@pquiring

Description

@pquiring

Describe the issue
Getting ClassCastException when using a SystemTray icon.

Steps to reproduce the issue
Sample code:

/**
 *
 * @author pquiring
 */

import java.awt.*;
import java.awt.image.*;

public class Main extends javax.swing.JFrame {

  SystemTray tray;
  Image image;
  TrayIcon icon;

  /**
   * Creates new form Main
   */
  public Main() {
    initComponents();
    image = new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB);
    icon = new TrayIcon(image);
    tray = SystemTray.getSystemTray();
    try {
      tray.add(icon);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("Test");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jButton1)
        .addContainerGap(337, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jButton1)
        .addContainerGap(266, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
  }//GEN-LAST:event_jButton1ActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new Main().setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  // End of variables declaration//GEN-END:variables
}

Describe GraalVM and your environment:

  • GraalVM version 21.1.0
  • JDK major version: 11
  • 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 com.oracle.svm.jni.JNIJavaCallWrappers.jniInvoke_VA_LIST:Ljava_awt_event_MouseEvent_2_0002e_0003cinit_0003e_00028Ljava_awt_Component_2IJIIIIIIZI_00029V(JNIJavaCallWrappers.java:0)
        at sun.awt.windows.WToolkit.eventLoop(WToolkit.java)
        at sun.awt.windows.WToolkit.run(WToolkit.java:305)
        at java.lang.Thread.run(Thread.java:829)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:553)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)

NOTE : You must copy ${java.home}\lib\*font* to .\lib

Making great progress with AWT!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions