JAL-4121 Added a NPE check caught in tests. Added --gui to relevant tests.
[jalview.git] / src / jalview / bin / Jalview.java
index 8fa4ac7..7535f60 100755 (executable)
@@ -451,11 +451,14 @@ public class Jalview
                 .getDefault("DEFAULT_CLOSE_EXTERNAL_VIEWERS", false)
                 || Cache.getDefault("ALWAYS_CLOSE_EXTERNAL_VIEWERS", false);
         StructureViewerBase.setQuitClose(closeExternal);
-        for (JInternalFrame frame : Desktop.desktop.getAllFrames())
+        if (desktop != null)
         {
-          if (frame instanceof StructureViewerBase)
+          for (JInternalFrame frame : Desktop.desktop.getAllFrames())
           {
-            ((StructureViewerBase) frame).closeViewer(closeExternal);
+            if (frame instanceof StructureViewerBase)
+            {
+              ((StructureViewerBase) frame).closeViewer(closeExternal);
+            }
           }
         }
 
@@ -532,17 +535,17 @@ public class Jalview
         Jalview.exit(null, 0);
       }
 
-      if (bootstrapArgs.contains(Arg.HEADLESS))
+      // new CLI
+      headlessArg = isHeadless(bootstrapArgs);
+      if (headlessArg)
       {
         System.setProperty("java.awt.headless", "true");
-        // new
-        headlessArg = bootstrapArgs.getBoolean(Arg.HEADLESS);
       }
+      // old CLI
       if (aparser.contains("nodisplay") || aparser.contains("nogui")
               || aparser.contains("headless"))
       {
         System.setProperty("java.awt.headless", "true");
-        // old
         headless = true;
       }
       // anything else!
@@ -1883,4 +1886,26 @@ public class Jalview
     System.out.println("[TESTOUTPUT] arg "
             + (yes ? a.argString() : a.negateArgString()) + " was set");
   }
+
+  private static boolean isHeadless(BootstrapArgs bootstrapArgs)
+  {
+    if (bootstrapArgs == null)
+    {
+      return false;
+    }
+    boolean isHeadless = false;
+    if (bootstrapArgs.contains(Arg.GUI))
+    {
+      isHeadless = !bootstrapArgs.getBoolean(Arg.GUI);
+    }
+    else if (bootstrapArgs.contains(Arg.HEADLESS))
+    {
+      isHeadless = bootstrapArgs.getBoolean(Arg.HEADLESS);
+    }
+    else if (bootstrapArgs.argsHaveOption(Opt.OUTPUTFILE))
+    {
+      isHeadless = true;
+    }
+    return isHeadless;
+  }
 }