Starting with Java 17.0.12 and 21.0.4 Java fails with a Headless exception when running a full GUI app via RDP and the RDP session is disconnected. The remote desktop is running and can be reconnected with. All non-Java apps are still running and updating their UI's just fine. It is only Java GUI apps that are crashing.
If the Java program attempts to do GUI operations such as create a modal progress bar while the RDP session is disconnected, a Headless exception is thrown, even though the environment is not headless, just disconnected.
This seems to be related to https://bugs.openjdk.org/browse/JDK-8340992 and https://bugs.openjdk.org/browse/JDK-8336862 but there are no workarounds provided.
Is there a workaround that can be done runtime?
The only workarounds we can come up with are:
This would also seem to be breaking of the "contract" where minor point releases are not supposed to cause breaking changes.
We mostly use simple apps composed of JFrames, JTextComponents, and ProgressMonitor with a couple of self-hosted JediTerm applications.
Code demonstrating issue. MUST RUN IN RDP SESSION:
import javax.swing.*;
import java.awt.*;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) throws InterruptedException {
var delay = 10;
var isHeadless = GraphicsEnvironment.isHeadless();
System.out.printf("1) Is headless: %s.%n", isHeadless);
System.out.printf("Sleeping %,d seconds.%n", delay);
System.out.printf("""
DISCONNECT YOUR REMOTE DESKTOP CONNECTION NOW!
RECONNECT AFTER %,d SECONDS.
""", delay);
Thread.sleep(delay * 1_000);
System.out.println("Drawing progress bar");
ProgressMonitor p = new ProgressMonitor(null, "Test progressbar", "My note", 0, 100);
for (int i = 0; i < 100; i++) {
p.setProgress(i);
Thread.sleep(250);
}
isHeadless = GraphicsEnvironment.isHeadless();
System.out.printf("2) Is headless: %s.%n", isHeadless);
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}
Errors:
"C:\Program Files\Eclipse Adoptium\jdk-21.0.5.11-hotspot\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.1\lib\idea_rt.jar=59072:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\admin.NEWSRX\git\rdp-crashes\out\production\rdp-crashes Main
Picked up JAVA_TOOL_OPTIONS: -Xmx6144m -Dlog4j2.formatMsgNoLookups=true
1) Is headless: false.
Sleeping 10 seconds.
DISCONNECT YOUR REMOTE DESKTOP CONNECTION NOW!
RECONNECT AFTER 10 SECONDS.
Drawing progress bar
Exception in thread "main" java.awt.AWTError: no screen devices
at java.desktop/sun.awt.Win32GraphicsEnvironment.getDefaultScreenDevice(Win32GraphicsEnvironment.java:101)
at java.desktop/sun.font.FontDesignMetrics.getDefaultFrc(FontDesignMetrics.java:157)
at java.desktop/sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:274)
at java.desktop/sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1242)
at java.desktop/javax.swing.JComponent.getFontMetrics(JComponent.java:1700)
at java.desktop/javax.swing.plaf.basic.BasicGraphicsUtils.getPreferredButtonSize(BasicGraphicsUtils.java:360)
at java.desktop/javax.swing.plaf.basic.BasicButtonUI.getPreferredSize(BasicButtonUI.java:542)
at java.desktop/javax.swing.plaf.basic.BasicButtonUI.getMinimumSize(BasicButtonUI.java:532)
at java.desktop/javax.swing.JComponent.getMinimumSize(JComponent.java:1814)
at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.addButtonComponents(BasicOptionPaneUI.java:818)
at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.createButtonArea(BasicOptionPaneUI.java:751)
at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.installComponents(BasicOptionPaneUI.java:206)
at java.desktop/javax.swing.plaf.basic.BasicOptionPaneUI.installUI(BasicOptionPaneUI.java:160)
at java.desktop/javax.swing.JComponent.setUI(JComponent.java:740)
at java.desktop/javax.swing.JOptionPane.setUI(JOptionPane.java:1858)
at java.desktop/javax.swing.JOptionPane.updateUI(JOptionPane.java:1880)
at java.desktop/javax.swing.JOptionPane.<init>(JOptionPane.java:1845)
at java.desktop/javax.swing.ProgressMonitor$ProgressOptionPane.<init>(ProgressMonitor.java:161)
at java.desktop/javax.swing.ProgressMonitor.setProgress(ProgressMonitor.java:291)
at Main.main(Main.java:20)
Process finished with exit code 1
-Mike/NewsRx
Per https://github.com/adoptium/adoptium-support/issues/1203#issuecomment-2544919811 the only known "fix" is to downgrade to 21.0.3 or 17.0.11 on Windows desktop systems.
Per mjschwaiger there is nothing on track to fix the breaking change introduced after 21.0.3 at this time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With