Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8369247

java.awt.HeadlessException: The application does not have desktop access

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Running on an Azure VM

      A DESCRIPTION OF THE PROBLEM :
      Running on a VM in a CI/CD pipeline I get this error when running anything that requires Swing:
      java.awt.HeadlessException: The application does not have desktop access, but this program performed an operation which requires it

      If I add "-Djava.awt.headless=false" I get a slightly different error:
      java.awt.AWTError: no screen devices

      This works fine in Java 21.

      I found this issue: https://bugs.openjdk.org/browse/JDK-8336862 which is still open. It mentions java 21 being affected even though it works for me.

      REGRESSION : Last worked in version 21.0.8

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run any java swing application on a cloud VM.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No error
      ACTUAL -
      java.awt.HeadlessException: The application does not have desktop access, but this program performed an operation which requires it

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.util.Timer;
      import java.util.TimerTask;

      public class HelloWorld {
          public static void main(String[] args) {
              int autoCloseSeconds = 0;

              if (args.length > 0) {
                  try {
                      autoCloseSeconds = Integer.parseInt(args[0]);
                      if (autoCloseSeconds < 0) {
                          System.err.println("Error: Auto-close seconds must be non-negative");
                          System.exit(1);
                      }
                  } catch (NumberFormatException e) {
                      System.err.println("Error: Invalid number format for auto-close seconds");
                      System.err.println("Usage: java HelloWorld [auto-close-seconds]");
                      System.exit(1);
                  }
              }

              JFrame frame = new JFrame("Hello World");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JLabel label = new JLabel("Hello World", JLabel.CENTER);
              frame.add(label);
              frame.pack();
              frame.setSize(300, 200);
              // Center the frame on screen
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);

              if (autoCloseSeconds > 0) {
                  System.out.println("Window will close automatically in " + autoCloseSeconds + " seconds");
                  Timer timer = new Timer();
                  timer.schedule(new TimerTask() {
                      @Override
                      public void run() {
                          System.out.println("Auto-closing window...");
                          frame.dispose();
                          System.exit(0);
                      }
                  }, autoCloseSeconds * 1000);
              }
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: