Skip to content

Commit aa50625

Browse files
committed
8286447: [Linux] AWT should start in Headless mode if headful AWT library not installed
Reviewed-by: asemenyuk, kcr
1 parent 655500a commit aa50625

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/java.desktop/unix/classes/sun/awt/PlatformGraphicsInfo.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package sun.awt;
2727

28+
import java.io.File;
2829
import java.awt.GraphicsEnvironment;
2930
import java.awt.Toolkit;
3031
import java.security.AccessController;
@@ -43,17 +44,42 @@ public static Toolkit createToolkit() {
4344
/**
4445
* Called from java.awt.GraphicsEnvironment when
4546
* to check if on this platform, the JDK should default to
46-
* headless mode, in the case the application did specify
47+
* headless mode, in the case the application did not specify
4748
* a value for the java.awt.headless system property.
4849
*/
4950
@SuppressWarnings("removal")
5051
public static boolean getDefaultHeadlessProperty() {
51-
return
52+
boolean noDisplay =
5253
AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
5354

5455
final String display = System.getenv("DISPLAY");
55-
return display == null || display.trim().isEmpty();
56+
return display == null || display.trim().isEmpty();
57+
});
58+
if (noDisplay) {
59+
return true;
60+
}
61+
/*
62+
* If we positively identify a separate headless library support being present
63+
* but no corresponding headful library, then we can support headless but
64+
* not headful, so report that back to the caller.
65+
* This does require duplication of knowing the name of the libraries
66+
* also in libawt's OnLoad() but we need to make sure that the Java
67+
* code is also set up as headless from the start - it is not so easy
68+
* to try headful and then unwind that and then retry as headless.
69+
*/
70+
boolean headless =
71+
AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
72+
String[] libDirs = System.getProperty("sun.boot.library.path", "").split(":");
73+
for (String libDir : libDirs) {
74+
File headlessLib = new File(libDir, "libawt_headless.so");
75+
File xawtLib = new File(libDir, "libawt_xawt.so");
76+
if (headlessLib.exists() && !xawtLib.exists()) {
77+
return true;
78+
}
79+
}
80+
return false;
5681
});
82+
return headless;
5783
}
5884

5985
/**
@@ -63,7 +89,11 @@ public static boolean getDefaultHeadlessProperty() {
6389
*/
6490
public static String getDefaultHeadlessMessage() {
6591
return
66-
"\nNo X11 DISPLAY variable was set,\n" +
67-
"but this program performed an operation which requires it.";
92+
"""
93+
94+
No X11 DISPLAY variable was set,
95+
or no headful library support was found,
96+
but this program performed an operation which requires it,
97+
""";
6898
}
6999
}

0 commit comments

Comments
 (0)