Java + Windows RDP disconnect causes Java Headless Exception
Please provide a brief summary of the bug
I've also posted this at stackoverflow in case someone there can provide a short term work around. https://stackoverflow.com/q/79265762/1341731
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:
- Rewrite everything in another language. Not a small undertaking.
- Downgrade all Java/JDK instances to 21.0.3 and not apply any security updates.
- Lose all work and start over for any RDP disconnects caused by intentional user disconnect, internet issues, etc. This is where we are at now and it is causing production issues.
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.
Did you test with the latest update version?
- [X] Yes
Please provide steps to reproduce where possible
Create an app with opens and closes windows with time delays. Start the app on Windows in an RDP session. Disconnect from the RDP session. Wait till the app should have opened and closed any test windows. Reconnect to the RDP session.
Expected Results
The app to create UI components in the disconnected RDP session, just like all other non-Java apps are capable of.
Actual Results
GUI apps should keep running when a remote desktop session is disconnected. They should not suddenly fail reporting that GUI operations are not possible.
What Java Version are you using?
Picked up JAVA_TOOL_OPTIONS: -Xmx6144m -Dlog4j2.formatMsgNoLookups=true openjdk version "21.0.5" 2024-10-15 LTS OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode, sharing) PS C:\Users\admin.NEWSRX>
What is your operating system and platform?
Windows specifications:
- Edition: Windows 10 Pro
- Version: 22H2
- Installed on: 2021-04-23
- OS build: 19045.5131
How did you install Java?
- AdoptOpenJDK
https://github.com/tushev/aojdk-updatewatcher/wiki
Did it work before?
Works with version 21.0.3
Did you test with other Java versions?
Every point release after 21.0.3 that AdoptOpenJDK reported in need of updating.
Relevant log output
No response
Can this issue be monkey-patched?
Code demonstrating issue:
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);
}
}
}
Error log:
"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
Looks to be https://bugs.openjdk.org/browse/JDK-8336862
Does setting -Djava.awt.headless=false help?
No this does not help.
"C:\Program Files\Eclipse Adoptium\jdk-21.0.5.11-hotspot\bin\java.exe" -Djava.awt.headless=false "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.1\lib\idea_rt.jar=59410: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
Hrmmmm... just tried using JetBrains JDK in Intellij version 21.0.5 and the problem does not occur.
OK
So we've downgraded all the physical desktop systems to 21.0.3 and RDP disconnects no longer cause Java to crash.
As we use AdoptOpenJDK, we told it to skip the current 21.0.5 release for updating.
-Mike/NewsRx
Hrmmmm... just tried using JetBrains JDK in Intellij version 21.0.5 and the problem does not occur.
The reason why this works in JetBrains Runtime (JBR) is because the problem has been "solved" directly in JBR. The "fix" was to undo the changes from JDK-8336862. See JBR-7683 and commit bb54739. Seems to work again with JBR 21.0.5 b709 and later.
- https://youtrack.jetbrains.com/issue/JBR-7683
- https://github.com/JetBrains/JetBrainsRuntime/commit/bb54739eb242dc32b2acd369199eef3c6ca435eb
However, there are no changes and no proposals for the OpenJDK at the present time.
- https://bugs.openjdk.org/browse/JDK-8336862
- https://bugs.openjdk.org/browse/JDK-8185862
Currently, the only known workaround is to stick with Java 21.0.3 (Java 17.0.11). However, as it does not provide fixes for new vulnerabilities and other issues, it is not a long-term solution.
I'm working on a backout of JDK-8185862: JDK-8348625
We have several customers affected by this issue on an eclipse RCP based application + RDP.
Are there currently any fixes for Java 23/24 in the works as well?
We have several customers affected by this issue on an eclipse RCP based application + RDP.
Are there currently any fixes for Java 23/24 in the works as well?
I will have a look at this. I think the RDP issue can be addressed.
@RealCLanger: Thanks for reverting the changes of the issue JDK-8185862 in issue JDK-8348625 (see 1359) for JDK 21 (jdk21u-dev).
@GoeLin: What are the next steps to ensure that these changes are also included in JDK 17 (jdk17u-dev)? Will JDK 17 have a separate backport issue? We hope this will happen in time for the next release update in April 2025 (JDK 17.0.15).
@RealCLanger: Thanks for reverting the changes of the issue JDK-8185862 in issue JDK-8348625 (see 1359) for JDK 21 (jdk21u-dev).
@GoeLin: What are the next steps to ensure that these changes are also included in JDK 17 (jdk17u-dev)? Will JDK 17 have a separate backport issue? We hope this will happen in time for the next release update in April 2025 (JDK 17.0.15).
No worries, I'm working on the backort to jdk17u, in time for the April update. 😄
Thanks for reverting the changes of the issue JDK-8185862 in issue JDK-8348625 (see 1359) for JDK 21 (jdk21u-dev).
In the meantime, please test JDK 21 ea builds (e.g. from Temurin) to see if the issues you see are gone:
https://api.adoptium.net/v3/binary/latest/21/ea/windows/x64/jdk/hotspot/normal/eclipse
This should get you an 21.0.7+1 build (which has the fix) in the next couple of days (it's currently pointing to 21.0.6+6 - so be patient).
Already tested today with both jdk-21.0.6+7-jre and jdk-21.0.7+1-jre.
❌ The problem can be reproduced using jdk-21.0.6+7-jre.
✅ But the issue is not reproducible with jdk-21.0.7+1-jre.
The following build was used for this test:
- Platform:
Windows x64 - Type:
JRE - Build:
21.0.7-beta+1-ea - Date:
2025-02-10 - Source: https://adoptium.net/de/temurin/nightly/
Thanks for testing! We'll close this issue once the 21.0.7 and 17.0.15 releases are done.
Most 21.0.7 and 17.0.15 releases have shipped now. Closing.
Thanks for the fix!
What's the current situation for later Java versions, e.g. JDK 25? I can't seem to find any information about a fix for this issue for the later versions, although I'm quite sure they were affected previously. (At least we had reports of similar crashes from users on Java 25.)
@RealCLanger