File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed
src/main/java/org/scijava/ui Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -116,6 +116,14 @@ public final class DefaultUIService extends AbstractService implements
116116 /** The default user interface to use, if one is not explicitly specified. */
117117 private UserInterface defaultUI ;
118118
119+ /**
120+ * When true, {@link #isHeadless()} will return true regardless of the value
121+ * of the {@code java.awt.headless} system property. When false, {@link
122+ * #isHeadless()} matches the global JVM headless state defined by {@code
123+ * java.awt.headless}.
124+ */
125+ private boolean forceHeadless ;
126+
119127 private boolean activationInvocationPending = false ;
120128
121129 // -- UIService methods --
@@ -178,12 +186,14 @@ public boolean isVisible(final String name) {
178186
179187 @ Override
180188 public void setHeadless (final boolean headless ) {
181- System . setProperty ( "java.awt.headless" , String . valueOf ( headless )) ;
189+ forceHeadless = headless ;
182190 }
183191
184192 @ Override
185193 public boolean isHeadless () {
186- return Boolean .getBoolean ("java.awt.headless" );
194+ // NB: We do not use java.awt.GraphicsConfiguration.isHeadless()
195+ // because scijava-common eschews java.awt.* classes when possible.
196+ return forceHeadless || Boolean .getBoolean ("java.awt.headless" );
187197 }
188198
189199 @ Override
Original file line number Diff line number Diff line change @@ -101,7 +101,22 @@ public interface UIService extends SciJavaService {
101101 /** Gets whether the UI with the given name or class name is visible. */
102102 boolean isVisible (String name );
103103
104- /** Sets whether the application is running in headless mode (no UI). */
104+ /**
105+ * Sets whether the application should run in headless mode (no UI).
106+ * <p>
107+ * Note that if the system itself is headless—which can be detected via
108+ * the {@code java.awt.headless} system property or by calling
109+ * {@code java.awt.GraphicsEnvironment.isHeadless()}—then calling
110+ * {@code setHeadless(false)} will have no effect; the system will still be
111+ * headless, and {@link #isHeadless()} will still return true.
112+ * </p>
113+ * <p>
114+ * But if the system itself is <em>not</em> headless, calling
115+ * {@code setHeadless(true)} will force {@link #isHeadless()} to return true,
116+ * instructing the application to behave in a headless manner insofar as it
117+ * can.
118+ * </p>
119+ */
105120 void setHeadless (boolean isHeadless );
106121
107122 /** Gets whether the UI is running in headless mode (no UI). */
You can’t perform that action at this time.
0 commit comments