JAL-629 correct a timeout. Correct passing html flag.
[jalview.git] / src / jalview / bin / Jalview.java
index efaf86d..7518fce 100755 (executable)
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
- * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- * 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.bin;
 
+import java.awt.Color;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.PrintStream;
 import java.io.PrintWriter;
-import java.lang.reflect.Constructor;
 import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.URLDecoder;
 import java.security.AllPermission;
 import java.security.CodeSource;
 import java.security.PermissionCollection;
 import java.security.Permissions;
 import java.security.Policy;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Vector;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+import javax.swing.JDialog;
+import javax.swing.JFrame;
+import javax.swing.JInternalFrame;
+import javax.swing.JOptionPane;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UIManager.LookAndFeelInfo;
+import javax.swing.UnsupportedLookAndFeelException;
 
-import javax.swing.*;
+import com.formdev.flatlaf.FlatLightLaf;
+import com.formdev.flatlaf.themes.FlatMacLightLaf;
+import com.formdev.flatlaf.util.SystemInfo;
+import com.threerings.getdown.util.LaunchUtil;
 
-import jalview.gui.*;
+//import edu.stanford.ejalbert.launching.IBrowserLaunching;
+import groovy.lang.Binding;
+import groovy.util.GroovyScriptEngine;
+import jalview.bin.argparser.Arg;
+import jalview.bin.argparser.Arg.Opt;
+import jalview.bin.argparser.Arg.Type;
+import jalview.bin.argparser.ArgParser;
+import jalview.bin.argparser.BootstrapArgs;
+import jalview.ext.so.SequenceOntology;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
+import jalview.gui.JvOptionPane;
+import jalview.gui.PromptUserConfig;
+import jalview.gui.QuitHandler;
+import jalview.gui.QuitHandler.QResponse;
+import jalview.gui.StructureViewerBase;
+import jalview.io.AppletFormatAdapter;
+import jalview.io.BioJsHTMLOutput;
+import jalview.io.DataSourceType;
+import jalview.io.FileFormat;
+import jalview.io.FileFormatException;
+import jalview.io.FileFormatI;
+import jalview.io.FileFormats;
+import jalview.io.FileLoader;
+import jalview.io.HtmlSvgOutput;
+import jalview.io.IdentifyFile;
+import jalview.io.NewickFile;
+import jalview.io.exceptions.ImageOutputException;
+import jalview.io.gff.SequenceOntologyFactory;
+import jalview.schemes.ColourSchemeI;
+import jalview.schemes.ColourSchemeProperty;
+import jalview.util.ChannelProperties;
+import jalview.util.HttpUtils;
+import jalview.util.LaunchUtils;
+import jalview.util.MessageManager;
 import jalview.util.Platform;
+import jalview.ws.jws2.Jws2Discoverer;
 
 /**
  * Main class for Jalview Application <br>
  * <br>
- * start with java -Djava.ext.dirs=$PATH_TO_LIB$ jalview.bin.Jalview
+ * start with: java -classpath "$PATH_TO_LIB$/*:$PATH_TO_CLASSES$" \
+ * jalview.bin.Jalview
+ * 
+ * or on Windows: java -classpath "$PATH_TO_LIB$/*;$PATH_TO_CLASSES$" \
+ * jalview.bin.Jalview jalview.bin.Jalview
+ * 
+ * (ensure -classpath arg is quoted to avoid shell expansion of '*' and do not
+ * embellish '*' to e.g. '*.jar')
  * 
  * @author $author$
  * @version $Revision$
@@ -52,20 +122,125 @@ public class Jalview
 {
   static
   {
-    // grab all the rights we can the JVM
-    Policy.setPolicy(new Policy()
+    Platform.getURLCommandArguments();
+    Platform.addJ2SDirectDatabaseCall("https://www.jalview.org");
+    Platform.addJ2SDirectDatabaseCall("http://www.jalview.org");
+    Platform.addJ2SDirectDatabaseCall("http://www.compbio.dundee.ac.uk");
+    Platform.addJ2SDirectDatabaseCall("https://www.compbio.dundee.ac.uk");
+  }
+
+  /*
+   * singleton instance of this class
+   */
+  private static Jalview instance;
+
+  private Desktop desktop;
+
+  protected Commands cmds;
+
+  public static AlignFrame currentAlignFrame;
+
+  private ArgParser argparser = null;
+
+  private BootstrapArgs bootstrapArgs = null;
+
+  private boolean QUIET = false;
+
+  public static boolean quiet()
+  {
+    return Jalview.getInstance() != null && Jalview.getInstance().QUIET;
+  }
+
+  static
+  {
+    if (!Platform.isJS())
+    /**
+     * Java only
+     * 
+     * @j2sIgnore
+     */
     {
-      public PermissionCollection getPermissions(CodeSource codesource)
+      // grab all the rights we can for the JVM
+      Policy.setPolicy(new Policy()
       {
-        Permissions perms = new Permissions();
-        perms.add(new AllPermission());
-        return (perms);
-      }
+        @Override
+        public PermissionCollection getPermissions(CodeSource codesource)
+        {
+          Permissions perms = new Permissions();
+          perms.add(new AllPermission());
+          return (perms);
+        }
+
+        @Override
+        public void refresh()
+        {
+        }
+      });
+    }
+  }
 
-      public void refresh()
+  /**
+   * keep track of feature fetching tasks.
+   * 
+   * @author JimP
+   * 
+   */
+  class FeatureFetcher
+  {
+    /*
+     * TODO: generalise to track all jalview events to orchestrate batch processing
+     * events.
+     */
+
+    private int queued = 0;
+
+    private int running = 0;
+
+    public FeatureFetcher()
+    {
+
+    }
+
+    public void addFetcher(final AlignFrame af,
+            final Vector<String> dasSources)
+    {
+      final long id = System.currentTimeMillis();
+      queued++;
+      final FeatureFetcher us = this;
+      new Thread(new Runnable()
       {
-      }
-    });
+
+        @Override
+        public void run()
+        {
+          synchronized (us)
+          {
+            queued--;
+            running++;
+          }
+
+          af.setProgressBar(MessageManager
+                  .getString("status.das_features_being_retrived"), id);
+          af.featureSettings_actionPerformed(null);
+          af.setProgressBar(null, id);
+          synchronized (us)
+          {
+            running--;
+          }
+        }
+      }).start();
+    }
+
+    public synchronized boolean allFinished()
+    {
+      return queued == 0 && running == 0;
+    }
+
+  }
+
+  public static Jalview getInstance()
+  {
+    return instance;
   }
 
   /**
@@ -76,302 +251,718 @@ public class Jalview
    */
   public static void main(String[] args)
   {
-    System.out.println("Java version: "
-            + System.getProperty("java.version"));
-    System.out.println(System.getProperty("os.arch") + " "
-            + System.getProperty("os.name") + " "
-            + System.getProperty("os.version"));
-    if (new Platform().isAMac())
-    {
-      System.setProperty("com.apple.mrj.application.apple.menu.about.name",
-              "Jalview");
-      System.setProperty("apple.laf.useScreenMenuBar", "true");
+    // setLogging(); // BH - for event debugging in JavaScript
+    instance = new Jalview();
+    instance.doMain(args);
+  }
+
+  private static void logClass(String name)
+  {
+    // BH - for event debugging in JavaScript
+    ConsoleHandler consoleHandler = new ConsoleHandler();
+    consoleHandler.setLevel(Level.ALL);
+    Logger logger = Logger.getLogger(name);
+    logger.setLevel(Level.ALL);
+    logger.addHandler(consoleHandler);
+  }
+
+  @SuppressWarnings("unused")
+  private static void setLogging()
+  {
+
+    /**
+     * @j2sIgnore
+     * 
+     */
+    {
+      Console.outPrintln("not in js");
     }
 
-    ArgsParser aparser = new ArgsParser(args);
-    boolean headless = false;
+    // BH - for event debugging in JavaScript (Java mode only)
+    if (!Platform.isJS())
+    /**
+     * Java only
+     * 
+     * @j2sIgnore
+     */
+    {
+      Logger.getLogger("").setLevel(Level.ALL);
+      logClass("java.awt.EventDispatchThread");
+      logClass("java.awt.EventQueue");
+      logClass("java.awt.Component");
+      logClass("java.awt.focus.Component");
+      logClass("java.awt.focus.DefaultKeyboardFocusManager");
+    }
 
-    if (aparser.contains("help") || aparser.contains("h"))
-    {
-      System.out
-              .println("Usage: jalview -open [FILE] [OUTPUT_FORMAT] [OUTPUT_FILE]\n\n"
-                      + "-nodisplay\tRun Jalview without User Interface.\n"
-                      + "-props FILE\tUse the given Jalview properties file instead of users default.\n"
-                      + "-colour COLOURSCHEME\tThe colourscheme to be applied to the alignment\n"
-                      + "-annotations FILE\tAdd precalculated annotations to the alignment.\n"
-                      + "-tree FILE\tLoad the given newick format tree file onto the alignment\n"
-                      + "-features FILE\tUse the given file to mark features on the alignment.\n"
-                      + "-fasta FILE\tCreate alignment file FILE in Fasta format.\n"
-                      + "-clustal FILE\tCreate alignment file FILE in Clustal format.\n"
-                      + "-pfam FILE\tCreate alignment file FILE in PFAM format.\n"
-                      + "-msf FILE\tCreate alignment file FILE in MSF format.\n"
-                      + "-pileup FILE\tCreate alignment file FILE in Pileup format\n"
-                      + "-pir FILE\tCreate alignment file FILE in PIR format.\n"
-                      + "-blc FILE\tCreate alignment file FILE in BLC format.\n"
-                      + "-jalview FILE\tCreate alignment file FILE in Jalview format.\n"
-                      + "-png FILE\tCreate PNG image FILE from alignment.\n"
-                      + "-imgMap FILE\tCreate HTML file FILE with image map of PNG image.\n"
-                      + "-eps FILE\tCreate EPS file FILE from alignment.\n"
-                      + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires.\n"
-                      + "-noquestionnaire\tTurn off questionnaire check.\n"
-                      + "-nousagestats\tTurn off google analytics tracking for this session.\n"
-                      + "-sortbytree OR -nosortbytree\tEnable or disable sorting of the given alignment by the given tree\n"
-                      // +
-                      // "-setprop PROPERTY=VALUE\tSet the given Jalview property, after all other properties files have been read\n\t (quote the 'PROPERTY=VALUE' pair to ensure spaces are passed in correctly)"
-                      + "-dasserver nickname=URL\tAdd and enable a das server with given nickname\n\t\t\t(alphanumeric or underscores only) for retrieval of features for all alignments.\n"
-                      + "\t\t\tSources that also support the sequence command may be specified by prepending the URL with sequence:\n"
-                      + "\t\t\t e.g. sequence:http://localdas.somewhere.org/das/source)\n"
-                      + "-fetchfrom nickname\tQuery nickname for features for the alignments and display them.\n"
-                      // +
-                      // "-vdoc vamsas-document\tImport vamsas document into new session or join existing session with same URN\n"
-                      // + "-vses vamsas-session\tJoin session with given URN\n"
-                      + "-groovy FILE\tExecute groovy script in FILE, after all other arguments have been processed (if FILE is the text 'STDIN' then the file will be read from STDIN)\n"
-                      + "\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n");
-      System.exit(0);
-    }
-    Cache.loadProperties(aparser.getValue("props")); // must do this before
-    // anything else!
-    String defs = aparser.getValue("setprop");
-    while (defs != null)
-    {
-      int p = defs.indexOf('=');
-      if (p == -1)
+  }
+
+  /**
+   * @param args
+   */
+  void doMain(String[] args)
+  {
+    if (!Platform.isJS())
+    {
+      System.setSecurityManager(null);
+    }
+
+    if (args == null || args.length == 0 || (args.length == 1
+            && (args[0] == null || args[0].length() == 0)))
+    {
+      args = new String[] {};
+    }
+
+    // get args needed before proper ArgParser
+    bootstrapArgs = BootstrapArgs.getBootstrapArgs(args);
+
+    if (!Platform.isJS())
+    {
+      // are we being --quiet ?
+      if (bootstrapArgs.contains(Arg.QUIET))
       {
-        System.err.println("Ignoring invalid setprop argument : " + defs);
+        QUIET = true;
+        OutputStream devNull = new OutputStream()
+        {
+
+          @Override
+          public void write(int b)
+          {
+            // DO NOTHING
+          }
+        };
+        System.setOut(new PrintStream(devNull));
+        // redirecting stderr not working
+        if (bootstrapArgs.getList(Arg.QUIET).size() > 1)
+        {
+          System.setErr(new PrintStream(devNull));
+        }
       }
-      else
+
+      if (bootstrapArgs.contains(Arg.HELP)
+              || bootstrapArgs.contains(Arg.VERSION))
       {
-        System.out.println("Executing setprop argument: " + defs);
-        // DISABLED FOR SECURITY REASONS
-        // TODO: add a property to allow properties to be overriden by cli args
-        // Cache.setProperty(defs.substring(0,p), defs.substring(p+1));
+        QUIET = true;
       }
-      defs = aparser.getValue("setprop");
     }
-    if (aparser.contains("nodisplay"))
+
+    // set individual session preferences
+    if (bootstrapArgs.contains(Arg.P))
     {
-      System.setProperty("java.awt.headless", "true");
+      for (String kev : bootstrapArgs.getValueList(Arg.P))
+      {
+        if (kev == null)
+        {
+          continue;
+        }
+        int equalsIndex = kev.indexOf(ArgParser.EQUALS);
+        if (equalsIndex > -1)
+        {
+          String key = kev.substring(0, equalsIndex);
+          String val = kev.substring(equalsIndex + 1);
+          Cache.setSessionProperty(key, val);
+        }
+      }
     }
-    if (System.getProperty("java.awt.headless") != null
-            && System.getProperty("java.awt.headless").equals("true"))
+
+    // Move any new getdown-launcher-new.jar into place over old
+    // getdown-launcher.jar
+    String appdirString = System.getProperty("getdownappdir");
+    if (appdirString != null && appdirString.length() > 0)
     {
-      headless = true;
+      final File appdir = new File(appdirString);
+      new Thread()
+      {
+
+        @Override
+        public void run()
+        {
+          LaunchUtil.upgradeGetdown(
+                  new File(appdir, "getdown-launcher-old.jar"),
+                  new File(appdir, "getdown-launcher.jar"),
+                  new File(appdir, "getdown-launcher-new.jar"));
+        }
+      }.start();
+    }
+
+    if (!quiet() || !bootstrapArgs.outputToStdout()
+            || bootstrapArgs.contains(Arg.VERSION))
+    {
+      Console.outPrintln(
+              "Java version: " + System.getProperty("java.version"));
+      Console.outPrintln("Java home: " + System.getProperty("java.home"));
+      Console.outPrintln("Java arch: " + System.getProperty("os.arch") + " "
+              + System.getProperty("os.name") + " "
+              + System.getProperty("os.version"));
+
+      String val = System.getProperty("sys.install4jVersion");
+      if (val != null)
+      {
+        Console.outPrintln("Install4j version: " + val);
+      }
+      val = System.getProperty("installer_template_version");
+      if (val != null)
+      {
+        Console.outPrintln("Install4j template version: " + val);
+      }
+      val = System.getProperty("launcher_version");
+      if (val != null)
+      {
+        Console.outPrintln("Launcher version: " + val);
+      }
+    }
+
+    if (Platform.isLinux() && LaunchUtils.getJavaVersion() < 11)
+    {
+      System.setProperty("flatlaf.uiScale", "1");
     }
 
+    // get bootstrap properties (mainly for the logger level)
+    Properties bootstrapProperties = Cache
+            .bootstrapProperties(bootstrapArgs.getValue(Arg.PROPS));
+
+    // report Jalview version
+    Cache.loadBuildProperties(
+            !quiet() || bootstrapArgs.contains(Arg.VERSION));
+
+    // stop now if only after --version
+    if (bootstrapArgs.contains(Arg.VERSION))
+    {
+      Jalview.exit(null, ExitCode.OK);
+    }
+
+    // old ArgsParser
+    ArgsParser aparser = new ArgsParser(args);
+
+    // old
+    boolean headless = false;
+    // new
+    boolean headlessArg = false;
+
     try
     {
-      Cache.initLogger();
-    } catch (java.lang.NoClassDefFoundError error)
+      String logLevel = null;
+      if (bootstrapArgs.contains(Arg.TRACE))
+      {
+        logLevel = "TRACE";
+      }
+      else if (bootstrapArgs.contains(Arg.DEBUG))
+      {
+        logLevel = "DEBUG";
+      }
+      if (logLevel == null && !(bootstrapProperties == null))
+      {
+        logLevel = bootstrapProperties.getProperty(Cache.JALVIEWLOGLEVEL);
+      }
+      Console.initLogger(logLevel);
+    } catch (NoClassDefFoundError error)
     {
       error.printStackTrace();
-      System.out
-              .println("\nEssential logging libraries not found."
-                      + "\nUse: java -Djava.ext.dirs=$PATH_TO_LIB$ jalview.bin.Jalview");
-      System.exit(0);
+      String message = "\nEssential logging libraries not found."
+              + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview";
+      Jalview.exit(message, ExitCode.OK);
     }
 
-    Desktop desktop = null;
+    // register SIGTERM listener
+    Runtime.getRuntime().addShutdownHook(new Thread()
+    {
+      @Override
+      public void run()
+      {
+        Console.debug("Running shutdown hook");
+        QuitHandler.startForceQuit();
+        boolean closeExternal = Cache
+                .getDefault("DEFAULT_CLOSE_EXTERNAL_VIEWERS", false)
+                || Cache.getDefault("ALWAYS_CLOSE_EXTERNAL_VIEWERS", false);
+        StructureViewerBase.setQuitClose(closeExternal);
+        if (desktop != null)
+        {
+          for (JInternalFrame frame : Desktop.desktop.getAllFrames())
+          {
+            if (frame instanceof StructureViewerBase)
+            {
+              ((StructureViewerBase) frame).closeViewer(closeExternal);
+            }
+          }
+        }
 
-    try
+        if (QuitHandler.gotQuitResponse() == QResponse.CANCEL_QUIT)
+        {
+          // Got to here by a SIGTERM signal.
+          // Note we will not actually cancel the quit from here -- it's too
+          // late -- but we can wait for saving files and close external viewers
+          // if configured.
+          // Close viewers/Leave viewers open
+          Console.debug("Checking for saving files");
+          QuitHandler.getQuitResponse(false);
+        }
+        else
+        {
+          Console.debug("Nothing more to do");
+        }
+        Console.debug("Exiting, bye!");
+        // shutdownHook cannot be cancelled, JVM will now halt
+      }
+    });
+
+    String usrPropsFile = bootstrapArgs.contains(Arg.PROPS)
+            ? bootstrapArgs.getValue(Arg.PROPS)
+            : aparser.getValue("props");
+    // if usrPropsFile == null, loadProperties will use the Channel
+    // preferences.file
+    Cache.loadProperties(usrPropsFile);
+    if (usrPropsFile != null)
     {
-      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
-    } catch (Exception ex)
+      Console.outPrintln(
+              "CMD [-props " + usrPropsFile + "] executed successfully!");
+      testoutput(bootstrapArgs, Arg.PROPS,
+              "test/jalview/bin/testProps.jvprops", usrPropsFile);
+    }
+
+    // --argfile=... -- OVERRIDES ALL NON-BOOTSTRAP ARGS
+    if (bootstrapArgs.contains(Arg.ARGFILE))
+    {
+      argparser = ArgParser.parseArgFiles(
+              bootstrapArgs.getValueList(Arg.ARGFILE),
+              bootstrapArgs.getBoolean(Arg.INITSUBSTITUTIONS),
+              bootstrapArgs);
+    }
+    else
     {
+      argparser = new ArgParser(args,
+              bootstrapArgs.getBoolean(Arg.INITSUBSTITUTIONS),
+              bootstrapArgs);
     }
 
-    if (!headless)
+    if (!Platform.isJS())
+    /**
+     * Java only
+     * 
+     * @j2sIgnore
+     */
     {
-      desktop = new Desktop();
-      desktop.setInBatchMode(true); // indicate we are starting up
-      desktop.setVisible(true);
-      desktop.startServiceDiscovery();
-      if (!aparser.contains("nousagestats"))
+      if (bootstrapArgs.contains(Arg.HELP))
+      {
+        List<Map.Entry<Type, String>> helpArgs = bootstrapArgs
+                .getList(Arg.HELP);
+        Console.outPrintln(Arg.usage(helpArgs.stream().map(e -> e.getKey())
+                .collect(Collectors.toList())));
+        Jalview.exit(null, ExitCode.OK);
+      }
+      if (aparser.contains("help") || aparser.contains("h"))
       {
-        startUsageStats(desktop);
+        /*
+         * Now using new usage statement.
+        showUsage();
+        */
+        Console.outPrintln(Arg.usage());
+        Jalview.exit(null, ExitCode.OK);
       }
-      if (!aparser.contains("noquestionnaire"))
+
+      // new CLI
+      headlessArg = bootstrapArgs.isHeadless();
+      if (headlessArg)
+      {
+        System.setProperty("java.awt.headless", "true");
+      }
+      // old CLI
+      if (aparser.contains("nodisplay") || aparser.contains("nogui")
+              || aparser.contains("headless"))
       {
-        String url = aparser.getValue("questionnaire");
-        if (url != null)
+        System.setProperty("java.awt.headless", "true");
+        headless = true;
+      }
+      // anything else!
+
+      // allow https handshakes to download intermediate certs if necessary
+      System.setProperty("com.sun.security.enableAIAcaIssuers", "true");
+
+      String jabawsUrl = bootstrapArgs.getValue(Arg.JABAWS);
+      if (jabawsUrl == null)
+        jabawsUrl = aparser.getValue("jabaws");
+      if (jabawsUrl != null)
+      {
+        try
         {
-          // Start the desktop questionnaire prompter with the specified
-          // questionnaire
-          Cache.log.debug("Starting questionnaire url at " + url);
-          desktop.checkForQuestionnaire(url);
-        }
-        else
+          Jws2Discoverer.getDiscoverer().setPreferredUrl(jabawsUrl);
+          Console.outPrintln(
+                  "CMD [-jabaws " + jabawsUrl + "] executed successfully!");
+          testoutput(bootstrapArgs, Arg.JABAWS,
+                  "http://www.compbio.dundee.ac.uk/jabaws", jabawsUrl);
+        } catch (MalformedURLException e)
         {
-          if (Cache.getProperty("NOQUESTIONNAIRES") == null)
-          {
-            // Start the desktop questionnaire prompter with the specified
-            // questionnaire
-            // String defurl =
-            // "http://anaplog.compbio.dundee.ac.uk/cgi-bin/questionnaire.pl";
-            // //
-            String defurl = "http://www.jalview.org/cgi-bin/questionnaire.pl";
-            Cache.log.debug("Starting questionnaire with default url: "
-                    + defurl);
-            desktop.checkForQuestionnaire(defurl);
+          jalview.bin.Console.errPrintln(
+                  "Invalid jabaws parameter: " + jabawsUrl + " ignored");
+        }
+      }
+    }
 
-          }
+    List<String> setprops = new ArrayList<>();
+    if (bootstrapArgs.contains(Arg.SETPROP))
+    {
+      setprops = bootstrapArgs.getValueList(Arg.SETPROP);
+    }
+    else
+    {
+      String sp = aparser.getValue("setprop");
+      while (sp != null)
+      {
+        setprops.add(sp);
+        sp = aparser.getValue("setprop");
+      }
+    }
+    for (String setprop : setprops)
+    {
+      int p = setprop.indexOf('=');
+      if (p == -1)
+      {
+        System.err
+                .println("Ignoring invalid setprop argument : " + setprop);
+      }
+      else
+      {
+        jalview.bin.Console
+                .errPrintln("Executing setprop argument: " + setprop);
+        if (Platform.isJS())
+        {
+          Cache.setProperty(setprop.substring(0, p),
+                  setprop.substring(p + 1));
         }
+        // DISABLED FOR SECURITY REASONS
+        // TODO: add a property to allow properties to be overriden by cli args
+        // Cache.setProperty(setprop.substring(0,p), setprop.substring(p+1));
       }
     }
+    if (System.getProperty("java.awt.headless") != null
+            && System.getProperty("java.awt.headless").equals("true"))
+    {
+      headless = true;
+    }
+    System.setProperty("http.agent", HttpUtils.getUserAgent());
 
-    String file = null, protocol = null, format = null, data = null;
-    jalview.io.FileLoader fileLoader = new jalview.io.FileLoader();
-    Vector getFeatures = null; // vector of das source nicknames to fetch
-    // features from
-    // loading is done.
-    String groovyscript = null; // script to execute after all loading is
-    // completed one way or another
-    // extract groovy argument and execute if necessary
-    groovyscript = aparser.getValue("groovy", true);
-    file = aparser.getValue("open", true);
+    try
+    {
+      Console.initLogger();
+    } catch (NoClassDefFoundError error)
+    {
+      error.printStackTrace();
+      String message = "\nEssential logging libraries not found."
+              + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview";
+      Jalview.exit(message, ExitCode.NO_LOGGING);
+    }
+    desktop = null;
 
-    if (file == null && desktop == null)
+    if (!(headless || headlessArg))
+      setLookAndFeel();
+
+    /*
+     * configure 'full' SO model if preferences say to, else use the default (full SO)
+     * - as JS currently doesn't have OBO parsing, it must use 'Lite' version
+     */
+    boolean soDefault = !Platform.isJS();
+    if (Cache.getDefault("USE_FULL_SO", soDefault))
     {
-      System.out.println("No files to open!");
-      System.exit(1);
+      SequenceOntologyFactory.setInstance(new SequenceOntology());
     }
-    String vamsasImport = aparser.getValue("vdoc"), vamsasSession = aparser
-            .getValue("vsess");
-    if (vamsasImport != null || vamsasSession != null)
+
+    if (!(headless || headlessArg))
     {
-      if (desktop == null || headless)
+      Desktop.nosplash = "false".equals(bootstrapArgs.getValue(Arg.SPLASH))
+              || aparser.contains("nosplash")
+              || Cache.getDefault("SPLASH", "true").equals("false");
+      desktop = new Desktop();
+      desktop.setInBatchMode(true); // indicate we are starting up
+
+      mixedCliWarning();
+
+      try
+      {
+        JalviewTaskbar.setTaskbar(this);
+      } catch (Exception e)
       {
-        System.out
-                .println("Headless vamsas sessions not yet supported. Sorry.");
-        System.exit(1);
+        Console.info("Cannot set Taskbar");
+        Console.error(e.getMessage());
+        // e.printStackTrace();
+      } catch (Throwable t)
+      {
+        Console.info("Cannot set Taskbar");
+        Console.error(t.getMessage());
+        // t.printStackTrace();
       }
-      // if we have a file, start a new session and import it.
-      boolean inSession = false;
-      if (vamsasImport != null)
+
+      // set Proxy settings before all the internet calls
+      Cache.setProxyPropertiesFromPreferences();
+
+      desktop.setVisible(true);
+
+      if (!Platform.isJS())
+      /**
+       * Java only
+       * 
+       * @j2sIgnore
+       */
       {
-        try
+
+        /**
+         * Check to see that the JVM version being run is suitable for the Java
+         * version this Jalview was compiled for. Popup a warning if not.
+         */
+        if (!LaunchUtils.checkJavaVersion())
         {
-          String viprotocol = jalview.io.AppletFormatAdapter
-                  .checkProtocol(vamsasImport);
-          if (viprotocol == jalview.io.FormatAdapter.FILE)
-          {
-            inSession = desktop.vamsasImport(new File(vamsasImport));
-          }
-          else if (viprotocol == jalview.io.FormatAdapter.URL)
+          Console.warn("The Java version being used (Java "
+                  + LaunchUtils.getJavaVersion()
+                  + ") may lead to problems. This installation of Jalview should be used with Java "
+                  + LaunchUtils.getJavaCompileVersion() + ".");
+
+          if (!LaunchUtils
+                  .getBooleanUserPreference("IGNORE_JVM_WARNING_POPUP"))
           {
-            inSession = desktop.vamsasImport(new URL(vamsasImport));
+            Object[] options = {
+                MessageManager.getString("label.continue") };
+            JOptionPane.showOptionDialog(null,
+                    MessageManager.formatMessage(
+                            "warning.wrong_jvm_version_message",
+                            LaunchUtils.getJavaVersion(),
+                            LaunchUtils.getJavaCompileVersion()),
+                    MessageManager
+                            .getString("warning.wrong_jvm_version_title"),
+                    JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
+                    null, options, options[0]);
           }
+        }
 
-        } catch (Exception e)
+        boolean webservicediscovery = bootstrapArgs
+                .getBoolean(Arg.WEBSERVICEDISCOVERY);
+        if (aparser.contains("nowebservicediscovery"))
+          webservicediscovery = false;
+        if (webservicediscovery)
         {
-          System.err.println("Exeption when importing " + vamsasImport
-                  + " as a vamsas document.");
-          e.printStackTrace();
+          desktop.startServiceDiscovery();
         }
-        if (!inSession)
+        else
         {
-          System.err.println("Failed to import " + vamsasImport
-                  + " as a vamsas document.");
+          testoutput(argparser, Arg.WEBSERVICEDISCOVERY);
         }
-        else
+
+        boolean usagestats = !bootstrapArgs.getBoolean(Arg.NOUSAGESTATS);
+        if (aparser.contains("nousagestats"))
+          usagestats = false;
+        if (usagestats)
         {
-          System.out.println("Imported Successfully into new session "
-                  + desktop.getVamsasApplication().getCurrentSession());
+          startUsageStats(desktop);
+          testoutput(argparser, Arg.NOUSAGESTATS);
         }
-      }
-      if (vamsasSession != null)
-      {
-        if (vamsasImport != null)
+        else
         {
-          // close the newly imported session and import the Jalview specific
-          // remnants into the new session later on.
-          desktop.vamsasStop_actionPerformed(null);
+          Console.outPrintln("CMD [-nousagestats] executed successfully!");
+          testoutput(argparser, Arg.NOUSAGESTATS);
         }
-        // now join the new session
-        try
+
+        boolean questionnaire = bootstrapArgs.getBoolean(Arg.QUESTIONNAIRE);
+        if (aparser.contains("noquestionnaire"))
+          questionnaire = false;
+        if (questionnaire)
         {
-          if (desktop.joinVamsasSession(vamsasSession))
+          String url = aparser.getValue("questionnaire");
+          if (url != null)
           {
-            System.out.println("Successfully joined vamsas session "
-                    + vamsasSession);
+            // Start the desktop questionnaire prompter with the specified
+            // questionnaire
+            Console.debug("Starting questionnaire url at " + url);
+            desktop.checkForQuestionnaire(url);
+            Console.outPrintln("CMD questionnaire[-" + url
+                    + "] executed successfully!");
           }
           else
           {
-            System.err.println("WARNING: Failed to join vamsas session "
-                    + vamsasSession);
+            if (Cache.getProperty("NOQUESTIONNAIRES") == null)
+            {
+              // Start the desktop questionnaire prompter with the specified
+              // questionnaire
+              // String defurl =
+              // "http://anaplog.compbio.dundee.ac.uk/cgi-bin/questionnaire.pl";
+              // //
+              String defurl = "https://www.jalview.org/cgi-bin/questionnaire.pl";
+              Console.debug(
+                      "Starting questionnaire with default url: " + defurl);
+              desktop.checkForQuestionnaire(defurl);
+            }
           }
-        } catch (Exception e)
+        }
+        else
+        {
+          Console.outPrintln(
+                  "CMD [-noquestionnaire] executed successfully!");
+          testoutput(argparser, Arg.QUESTIONNAIRE);
+        }
+
+        if ((!aparser.contains("nonews")
+                && Cache.getProperty("NONEWS") == null
+                && !"false".equals(bootstrapArgs.getValue(Arg.NEWS)))
+                || "true".equals(bootstrapArgs.getValue(Arg.NEWS)))
         {
-          System.err.println("ERROR: Failed to join vamsas session "
-                  + vamsasSession);
-          e.printStackTrace();
+          desktop.checkForNews();
         }
-        if (vamsasImport != null)
+
+        if (!aparser.contains("nohtmltemplates")
+                && Cache.getProperty("NOHTMLTEMPLATES") == null)
         {
-          // the Jalview specific remnants can now be imported into the new
-          // session at the user's leisure.
-          Cache.log
-                  .info("Skipping Push for import of data into existing vamsas session."); // TODO:
-          // enable
-          // this
-          // when
-          // debugged
-          // desktop.getVamsasApplication().push_update();
+          BioJsHTMLOutput.updateBioJS();
         }
       }
     }
+    else
+    {
+
+      if (getArgParser().isMixedStyle())
+      {
+        String warning = MessageManager.formatMessage(
+                "warning.using_mixed_command_line_arguments",
+                getArgParser().getMixedExamples());
+        Console.warn(warning);
+        Jalview.exit(
+                "Exiting due to mixed old and new command line arguments",
+                ExitCode.INVALID_ARGUMENT);
+      }
+      if (getArgParser().isOldStyle())
+      {
+        String warning = MessageManager
+                .getString("warning.using_old_command_line_arguments")
+                .replace("\n", " ")
+                + "https://www.jalview.org/help/html/features/commandline.html";
+        Console.warn(warning);
+      }
+
+    }
+
+    // Run Commands from cli
+    cmds = new Commands(argparser, headlessArg);
+    cmds.processArgs();
+    boolean commandsSuccess = cmds.argsWereParsed();
+
+    if (commandsSuccess)
+    {
+      if (headlessArg)
+      {
+        if (argparser.getBoolean(Arg.NOQUIT))
+        {
+          Console.warn(
+                  "Completed " + Arg.HEADLESS.getName() + " commands, but "
+                          + Arg.NOQUIT + " is set so not quitting!");
+        }
+        else
+        {
+          Jalview.exit("Successfully completed commands in headless mode",
+                  ExitCode.OK);
+        }
+      }
+      Console.info("Successfully completed commands");
+    }
+    else
+    {
+      if (headlessArg)
+      {
+        Jalview.exit("Error when running Commands in headless mode",
+                ExitCode.ERROR_RUNNING_COMMANDS);
+      }
+      Console.warn("Error when running commands");
+    }
+
+    // Check if JVM and compile version might cause problems and log if it
+    // might.
+    if (headless && !Platform.isJS() && !LaunchUtils.checkJavaVersion())
+    {
+      Console.warn("The Java version being used (Java "
+              + LaunchUtils.getJavaVersion()
+              + ") may lead to problems. This installation of Jalview should be used with Java "
+              + LaunchUtils.getJavaCompileVersion() + ".");
+    }
+
+    String file = null, data = null;
+
+    FileFormatI format = null;
+
+    DataSourceType protocol = null;
+
+    FileLoader fileLoader = new FileLoader(!headless);
+
+    String groovyscript = null; // script to execute after all loading is
+    // completed one way or another
+    // extract groovy argument and execute if necessary
+    groovyscript = aparser.getValue("groovy", true);
+    file = aparser.getValue("open", true);
+
+    if (file == null && desktop == null && !commandsSuccess)
+    {
+      Jalview.exit("No files to open!", ExitCode.NO_FILES);
+    }
+
     long progress = -1;
     // Finally, deal with the remaining input data.
     if (file != null)
     {
       if (!headless)
       {
-        desktop.setProgressBar("Processing commandline arguments...",
+        desktop.setProgressBar(
+                MessageManager
+                        .getString("status.processing_commandline_args"),
                 progress = System.currentTimeMillis());
       }
-      System.out.println("Opening file: " + file);
+      Console.outPrintln("CMD [-open " + file + "] executed successfully!");
 
-      if (!file.startsWith("http://"))
+      if (!Platform.isJS())
+      /**
+       * ignore in JavaScript -- can't just file existence - could load it?
+       * 
+       * @j2sIgnore
+       */
       {
-        if (!(new java.io.File(file)).exists())
+        if (!HttpUtils.startsWithHttpOrHttps(file))
         {
-          System.out.println("Can't find " + file);
-          if (headless)
+          if (!(new File(file)).exists())
           {
-            System.exit(1);
+            if (headless)
+            {
+              Jalview.exit(
+                      "Can't find file '" + file + "' in headless mode",
+                      ExitCode.FILE_NOT_FOUND);
+            }
+            Console.warn("Can't find file'" + file + "'");
           }
         }
       }
 
-      protocol = jalview.io.AppletFormatAdapter.checkProtocol(file);
+      protocol = AppletFormatAdapter.checkProtocol(file);
 
-      format = new jalview.io.IdentifyFile().Identify(file, protocol);
+      try
+      {
+        format = new IdentifyFile().identify(file, protocol);
+      } catch (FileFormatException e1)
+      {
+        // TODO ?
+      }
 
       AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol,
               format);
       if (af == null)
       {
-        System.out.println("error");
+        Console.outPrintln("error");
       }
       else
       {
-
+        setCurrentAlignFrame(af);
         data = aparser.getValue("colour", true);
         if (data != null)
         {
           data.replaceAll("%20", " ");
 
-          jalview.schemes.ColourSchemeI cs = jalview.schemes.ColourSchemeProperty
-                  .getColour(af.getViewport().getAlignment(), data);
+          ColourSchemeI cs = ColourSchemeProperty.getColourScheme(
+                  af.getViewport(), af.getViewport().getAlignment(), data);
 
-          if (cs == null)
+          if (cs != null)
           {
-            jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
-                    "white");
-            ucs.parseAppletParameter(data);
-            cs = ucs;
+            Console.outPrintln(
+                    "CMD [-colour " + data + "] executed successfully!");
           }
-
-          System.out.println("colour is " + data);
           af.changeColour(cs);
         }
 
@@ -380,245 +971,684 @@ public class Jalview
         if (data != null)
         {
           af.parseFeaturesFile(data,
-                  jalview.io.AppletFormatAdapter.checkProtocol(data));
-          System.out.println("Added " + data);
+                  AppletFormatAdapter.checkProtocol(data));
+          // Console.outPrintln("Added " + data);
+          Console.outPrintln(
+                  "CMD groups[-" + data + "]  executed successfully!");
         }
         data = aparser.getValue("features", true);
         if (data != null)
         {
           af.parseFeaturesFile(data,
-                  jalview.io.AppletFormatAdapter.checkProtocol(data));
-          System.out.println("Added " + data);
+                  AppletFormatAdapter.checkProtocol(data));
+          // Console.outPrintln("Added " + data);
+          Console.outPrintln(
+                  "CMD [-features " + data + "]  executed successfully!");
         }
 
         data = aparser.getValue("annotations", true);
         if (data != null)
         {
           af.loadJalviewDataFile(data, null, null, null);
-          System.out.println("Added " + data);
+          // Console.outPrintln("Added " + data);
+          Console.outPrintln(
+                  "CMD [-annotations " + data + "] executed successfully!");
         }
         // set or clear the sortbytree flag.
         if (aparser.contains("sortbytree"))
         {
-          af.getViewport().setSortByTree(true);
+          af.getViewport().setSortByTree(true);
+          if (af.getViewport().getSortByTree())
+          {
+            Console.outPrintln("CMD [-sortbytree] executed successfully!");
+          }
+        }
+        if (aparser.contains("no-annotation"))
+        {
+          af.getViewport().setShowAnnotation(false);
+          if (!af.getViewport().isShowAnnotation())
+          {
+            Console.outPrintln("CMD no-annotation executed successfully!");
+          }
+        }
+        if (aparser.contains("nosortbytree"))
+        {
+          af.getViewport().setSortByTree(false);
+          if (!af.getViewport().getSortByTree())
+          {
+            Console.outPrintln(
+                    "CMD [-nosortbytree] executed successfully!");
+          }
+        }
+        data = aparser.getValue("tree", true);
+        if (data != null)
+        {
+          try
+          {
+            Console.outPrintln(
+                    "CMD [-tree " + data + "] executed successfully!");
+            NewickFile nf = new NewickFile(data,
+                    AppletFormatAdapter.checkProtocol(data));
+            af.getViewport()
+                    .setCurrentTree(af.showNewickTree(nf, data).getTree());
+          } catch (IOException ex)
+          {
+            jalview.bin.Console.errPrintln("Couldn't add tree " + data);
+            ex.printStackTrace(System.err);
+          }
+        }
+
+        if (groovyscript != null)
+        {
+          // Execute the groovy script after we've done all the rendering stuff
+          // and before any images or figures are generated.
+          Console.outPrintln("Executing script " + groovyscript);
+          executeGroovyScript(groovyscript, af);
+          Console.outPrintln("CMD groovy[" + groovyscript
+                  + "] executed successfully!");
+          groovyscript = null;
+        }
+        String imageName = "unnamed.png";
+        while (aparser.getSize() > 1)
+        {
+          try
+          {
+            String outputFormat = aparser.nextValue();
+            file = aparser.nextValue();
+
+            if (outputFormat.equalsIgnoreCase("png"))
+            {
+              Console.outPrintln("Creating PNG image: " + file);
+              af.createPNG(new File(file));
+              imageName = (new File(file)).getName();
+              continue;
+            }
+            else if (outputFormat.equalsIgnoreCase("svg"))
+            {
+              Console.outPrintln("Creating SVG image: " + file);
+              File imageFile = new File(file);
+              imageName = imageFile.getName();
+              af.createSVG(imageFile);
+              continue;
+            }
+            else if (outputFormat.equalsIgnoreCase("html"))
+            {
+              File imageFile = new File(file);
+              imageName = imageFile.getName();
+              HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
+
+              Console.outPrintln("Creating HTML image: " + file);
+              htmlSVG.exportHTML(file);
+              continue;
+            }
+            else if (outputFormat.equalsIgnoreCase("biojsmsa"))
+            {
+              if (file == null)
+              {
+                jalview.bin.Console.errPrintln(
+                        "The output html file must not be null");
+                return;
+              }
+              try
+              {
+                BioJsHTMLOutput.refreshVersionInfo(
+                        BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
+              } catch (URISyntaxException e)
+              {
+                e.printStackTrace();
+              }
+              BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel);
+              Console.outPrintln(
+                      "Creating BioJS MSA Viwer HTML file: " + file);
+              bjs.exportHTML(file);
+              continue;
+            }
+            else if (outputFormat.equalsIgnoreCase("imgMap"))
+            {
+              Console.outPrintln("Creating image map: " + file);
+              af.createImageMap(new File(file), imageName);
+              continue;
+            }
+            else if (outputFormat.equalsIgnoreCase("eps"))
+            {
+              File outputFile = new File(file);
+              Console.outPrintln(
+                      "Creating EPS file: " + outputFile.getAbsolutePath());
+              af.createEPS(outputFile);
+              continue;
+            }
+
+            FileFormatI outFormat = null;
+            try
+            {
+              outFormat = FileFormats.getInstance().forName(outputFormat);
+            } catch (Exception formatP)
+            {
+              Console.outPrintln("Couldn't parse " + outFormat
+                      + " as a valid Jalview format string.");
+            }
+            if (outFormat != null)
+            {
+              if (!outFormat.isWritable())
+              {
+                Console.outPrintln(
+                        "This version of Jalview does not support alignment export as "
+                                + outputFormat);
+              }
+              else
+              {
+                af.saveAlignment(file, outFormat);
+                if (af.isSaveAlignmentSuccessful())
+                {
+                  Console.outPrintln("Written alignment in "
+                          + outFormat.getName() + " format to " + file);
+                }
+                else
+                {
+                  Console.outPrintln("Error writing file " + file + " in "
+                          + outFormat.getName() + " format!!");
+                }
+              }
+            }
+          } catch (ImageOutputException ioexc)
+          {
+            Console.outPrintln(
+                    "Unexpected error whilst exporting image to " + file);
+            ioexc.printStackTrace();
+          }
+
+        }
+
+        while (aparser.getSize() > 0)
+        {
+          Console.outPrintln("Unknown arg: " + aparser.nextValue());
+        }
+      }
+    }
+
+    AlignFrame startUpAlframe = null;
+    // We'll only open the default file if the desktop is visible.
+    // And the user
+    // ////////////////////
+
+    if (!Platform.isJS() && !headless && file == null
+            && Cache.getDefault("SHOW_STARTUP_FILE", true)
+            && !cmds.commandArgsProvided()
+            && !bootstrapArgs.getBoolean(Arg.NOSTARTUPFILE))
+    // don't open the startup file if command line args have been processed
+    // (&& !Commands.commandArgsProvided())
+    /**
+     * Java only
+     * 
+     * @j2sIgnore
+     */
+    {
+      file = Cache.getDefault("STARTUP_FILE",
+              Cache.getDefault("www.jalview.org", "https://www.jalview.org")
+                      + "/examples/exampleFile_2_7.jvp");
+      if (file.equals("http://www.jalview.org/examples/exampleFile_2_3.jar")
+              || file.equals(
+                      "http://www.jalview.org/examples/exampleFile_2_7.jar"))
+      {
+        file.replace("http:", "https:");
+        // hardwire upgrade of the startup file
+        file.replace("_2_3", "_2_7");
+        file.replace("2_7.jar", "2_7.jvp");
+        // and remove the stale setting
+        Cache.removeProperty("STARTUP_FILE");
+      }
+
+      protocol = AppletFormatAdapter.checkProtocol(file);
+
+      if (file.endsWith(".jar"))
+      {
+        format = FileFormat.Jalview;
+      }
+      else
+      {
+        try
+        {
+          format = new IdentifyFile().identify(file, protocol);
+        } catch (FileFormatException e)
+        {
+          // TODO what?
+        }
+      }
+
+      startUpAlframe = fileLoader.LoadFileWaitTillLoaded(file, protocol,
+              format);
+      // don't ask to save when quitting if only the startup file has been
+      // opened
+      Console.debug("Resetting up-to-date flag for startup file");
+      startUpAlframe.getViewport().setSavedUpToDate(true);
+      // extract groovy arguments before anything else.
+    }
+
+    // Once all other stuff is done, execute any groovy scripts (in order)
+    if (groovyscript != null)
+    {
+      if (Cache.groovyJarsPresent())
+      {
+        Console.outPrintln("Executing script " + groovyscript);
+        executeGroovyScript(groovyscript, startUpAlframe);
+      }
+      else
+      {
+        jalview.bin.Console.errPrintln(
+                "Sorry. Groovy Support is not available, so ignoring the provided groovy script "
+                        + groovyscript);
+      }
+    }
+    // and finally, turn off batch mode indicator - if the desktop still exists
+    if (desktop != null)
+    {
+      if (progress != -1)
+      {
+        desktop.setProgressBar(null, progress);
+      }
+      desktop.setInBatchMode(false);
+    }
+
+    cliWarning();
+  }
+
+  private static void setLookAndFeel()
+  {
+    if (!Platform.isJS())
+    /**
+     * Java only
+     * 
+     * @j2sIgnore
+     */
+    {
+      // property laf = "crossplatform", "system", "gtk", "metal", "nimbus",
+      // "mac" or "flat"
+      // If not set (or chosen laf fails), use the normal SystemLaF and if on
+      // Mac,
+      // try Quaqua/Vaqua.
+      String lafProp = System.getProperty("laf");
+      String lafSetting = Cache.getDefault("PREFERRED_LAF", null);
+      String laf = "none";
+      if (lafProp != null)
+      {
+        laf = lafProp;
+      }
+      else if (lafSetting != null)
+      {
+        laf = lafSetting;
+      }
+      boolean lafSet = false;
+      switch (laf)
+      {
+      case "crossplatform":
+        lafSet = setCrossPlatformLookAndFeel();
+        if (!lafSet)
+        {
+          Console.error("Could not set requested laf=" + laf);
+        }
+        break;
+      case "system":
+        lafSet = setSystemLookAndFeel();
+        if (!lafSet)
+        {
+          Console.error("Could not set requested laf=" + laf);
+        }
+        break;
+      case "gtk":
+        lafSet = setGtkLookAndFeel();
+        if (!lafSet)
+        {
+          Console.error("Could not set requested laf=" + laf);
+        }
+        break;
+      case "metal":
+        lafSet = setMetalLookAndFeel();
+        if (!lafSet)
+        {
+          Console.error("Could not set requested laf=" + laf);
         }
-        if (aparser.contains("nosortbytree"))
+        break;
+      case "nimbus":
+        lafSet = setNimbusLookAndFeel();
+        if (!lafSet)
         {
-          af.getViewport().setSortByTree(false);
+          Console.error("Could not set requested laf=" + laf);
         }
-        data = aparser.getValue("tree", true);
-        if (data != null)
+        break;
+      case "flat":
+        lafSet = setFlatLookAndFeel();
+        if (!lafSet)
         {
-          jalview.io.NewickFile fin = null;
-          try
-          {
-            fin = new jalview.io.NewickFile(data,
-                    jalview.io.AppletFormatAdapter.checkProtocol(data));
-            if (fin != null)
-            {
-              af.getViewport().setCurrentTree(
-                      af.ShowNewickTree(fin, data).getTree());
-              System.out.println("Added tree " + data);
-            }
-          } catch (IOException ex)
-          {
-            System.err.println("Couldn't add tree " + data);
-            ex.printStackTrace(System.err);
-          }
+          Console.error("Could not set requested laf=" + laf);
         }
-        // TODO - load PDB structure(s) to alignment JAL-629
-        // (associate with identical sequence in alignment, or a specified
-        // sequence)
-
-        getFeatures = checkDasArguments(aparser);
-        if (af != null && getFeatures != null)
+        break;
+      case "mac":
+        lafSet = setMacLookAndFeel();
+        if (!lafSet)
         {
-          FeatureFetcher ff = startFeatureFetching(getFeatures);
-          if (ff != null)
-            while (!ff.allFinished() || af.operationInProgress())
-            {
-              // wait around until fetching is finished.
-              try
-              {
-                Thread.sleep(100);
-              } catch (Exception e)
-              {
-
-              }
-            }
-          getFeatures = null; // have retrieved features - forget them now.
+          Console.error("Could not set requested laf=" + laf);
         }
-        if (groovyscript != null)
+        break;
+      case "none":
+        break;
+      default:
+        Console.error("Requested laf=" + laf + " not implemented");
+      }
+      if (!lafSet)
+      {
+        // Flatlaf default for everyone!
+        lafSet = setFlatLookAndFeel();
+        if (!lafSet)
         {
-          // Execute the groovy script after we've done all the rendering stuff
-          // and before any images or figures are generated.
-          if (jalview.bin.Cache.groovyJarsPresent())
-          {
-            System.out.println("Executing script " + groovyscript);
-            executeGroovyScript(groovyscript, new Object[]
-            { desktop, af });
-          }
-          else
-          {
-            System.err
-                    .println("Sorry. Groovy Support is not available, so ignoring the provided groovy script "
-                            + groovyscript);
-          }
-          groovyscript = null;
+          setSystemLookAndFeel();
         }
-        String imageName = "unnamed.png";
-        while (aparser.getSize() > 1)
+        if (Platform.isLinux())
         {
-          format = aparser.nextValue();
-          file = aparser.nextValue();
-
-          if (format.equalsIgnoreCase("png"))
-          {
-            af.createPNG(new java.io.File(file));
-            imageName = (new java.io.File(file)).getName();
-            System.out.println("Creating PNG image: " + file);
-            continue;
-          }
-          else if (format.equalsIgnoreCase("imgMap"))
-          {
-            af.createImageMap(new java.io.File(file), imageName);
-            System.out.println("Creating image map: " + file);
-            continue;
-          }
-          else if (format.equalsIgnoreCase("eps"))
-          {
-            System.out.println("Creating EPS file: " + file);
-            af.createEPS(new java.io.File(file));
-            continue;
-          }
-
-          if (af.saveAlignment(file, format))
-          {
-            System.out.println("Written alignment in " + format
-                    + " format to " + file);
-          }
-          else
-          {
-            System.out.println("Error writing file " + file + " in "
-                    + format + " format!!");
-          }
-
+          setLinuxLookAndFeel();
         }
-
-        while (aparser.getSize() > 0)
+        if (Platform.isMac())
         {
-          System.out.println("Unknown arg: " + aparser.nextValue());
+          setMacLookAndFeel();
         }
       }
     }
-    AlignFrame startUpAlframe = null;
-    // We'll only open the default file if the desktop is visible.
-    // And the user
-    // ////////////////////
-    if (!headless && file == null && vamsasImport == null
-            && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true))
+  }
+
+  private static boolean setCrossPlatformLookAndFeel()
+  {
+    boolean set = false;
+    try
     {
-      file = jalview.bin.Cache.getDefault(
-              "STARTUP_FILE",
-              jalview.bin.Cache.getDefault("www.jalview.org",
-                      "http://www.jalview.org")
-                      + "/examples/exampleFile_2_7.jar");
-      if (file.equals("http://www.jalview.org/examples/exampleFile_2_3.jar"))
-      {
-        // hardwire upgrade of the startup file
-        file.replace("_2_3.jar", "_2_7.jar");
-        // and remove the stale setting
-        jalview.bin.Cache.removeProperty("STARTUP_FILE");
-      }
+      UIManager.setLookAndFeel(
+              UIManager.getCrossPlatformLookAndFeelClassName());
+      set = true;
+    } catch (Exception ex)
+    {
+      Console.error("Unexpected Look and Feel Exception");
+      Console.error(ex.getMessage());
+      Console.debug(Cache.getStackTraceString(ex));
+    }
+    return set;
+  }
 
-      protocol = "File";
+  private static boolean setSystemLookAndFeel()
+  {
+    boolean set = false;
+    try
+    {
+      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+      set = true;
+    } catch (Exception ex)
+    {
+      Console.error("Unexpected Look and Feel Exception");
+      Console.error(ex.getMessage());
+      Console.debug(Cache.getStackTraceString(ex));
+    }
+    return set;
+  }
 
-      if (file.indexOf("http:") > -1)
+  private static boolean setSpecificLookAndFeel(String name,
+          String className, boolean nameStartsWith)
+  {
+    boolean set = false;
+    try
+    {
+      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
       {
-        protocol = "URL";
+        if (info.getName() != null && nameStartsWith
+                ? info.getName().toLowerCase(Locale.ROOT)
+                        .startsWith(name.toLowerCase(Locale.ROOT))
+                : info.getName().toLowerCase(Locale.ROOT)
+                        .equals(name.toLowerCase(Locale.ROOT)))
+        {
+          className = info.getClassName();
+          break;
+        }
       }
+      UIManager.setLookAndFeel(className);
+      set = true;
+    } catch (Exception ex)
+    {
+      Console.error("Unexpected Look and Feel Exception");
+      Console.error(ex.getMessage());
+      Console.debug(Cache.getStackTraceString(ex));
+    }
+    return set;
+  }
 
-      if (file.endsWith(".jar"))
+  private static boolean setGtkLookAndFeel()
+  {
+    return setSpecificLookAndFeel("gtk",
+            "com.sun.java.swing.plaf.gtk.GTKLookAndFeel", true);
+  }
+
+  private static boolean setMetalLookAndFeel()
+  {
+    return setSpecificLookAndFeel("metal",
+            "javax.swing.plaf.metal.MetalLookAndFeel", false);
+  }
+
+  private static boolean setNimbusLookAndFeel()
+  {
+    return setSpecificLookAndFeel("nimbus",
+            "javax.swing.plaf.nimbus.NimbusLookAndFeel", false);
+  }
+
+  private static boolean setFlatLookAndFeel()
+  {
+    boolean set = false;
+    if (SystemInfo.isMacOS)
+    {
+      try
       {
-        format = "Jalview";
+        UIManager.setLookAndFeel(
+                "com.formdev.flatlaf.themes.FlatMacLightLaf");
+        set = true;
+        Console.debug("Using FlatMacLightLaf");
+      } catch (ClassNotFoundException | InstantiationException
+              | IllegalAccessException | UnsupportedLookAndFeelException e)
+      {
+        Console.debug("Exception loading FlatLightLaf", e);
       }
-      else
+      System.setProperty("apple.laf.useScreenMenuBar", "true");
+      System.setProperty("apple.awt.application.name",
+              ChannelProperties.getProperty("app_name"));
+      System.setProperty("apple.awt.application.appearance", "system");
+      if (SystemInfo.isMacFullWindowContentSupported
+              && Desktop.desktop != null)
       {
-        format = new jalview.io.IdentifyFile().Identify(file, protocol);
+        Console.debug("Setting transparent title bar");
+        Desktop.desktop.getRootPane()
+                .putClientProperty("apple.awt.fullWindowContent", true);
+        Desktop.desktop.getRootPane()
+                .putClientProperty("apple.awt.transparentTitleBar", true);
+        Desktop.desktop.getRootPane()
+                .putClientProperty("apple.awt.fullscreenable", true);
       }
-
-      startUpAlframe = fileLoader.LoadFileWaitTillLoaded(file, protocol,
-              format);
-      getFeatures = checkDasArguments(aparser);
-      // extract groovy arguments before anything else.
+      SwingUtilities.invokeLater(() -> {
+        FlatMacLightLaf.setup();
+      });
+      Console.debug("Using FlatMacLightLaf");
+      set = true;
     }
-    // If the user has specified features to be retrieved,
-    // or a groovy script to be executed, do them if they
-    // haven't been done already
-    // fetch features for the default alignment
-    if (getFeatures != null)
+    if (!set)
     {
-      if (startUpAlframe != null)
+      try
       {
-        startFeatureFetching(getFeatures);
+        UIManager.setLookAndFeel("com.formdev.flatlaf.FlatLightLaf");
+        set = true;
+        Console.debug("Using FlatLightLaf");
+      } catch (ClassNotFoundException | InstantiationException
+              | IllegalAccessException | UnsupportedLookAndFeelException e)
+      {
+        Console.debug("Exception loading FlatLightLaf", e);
       }
+      // Windows specific properties here
+      SwingUtilities.invokeLater(() -> {
+        FlatLightLaf.setup();
+      });
+      Console.debug("Using FlatLightLaf");
+      set = true;
     }
-    // Once all other stuff is done, execute any groovy scripts (in order)
-    if (groovyscript != null)
+    else if (SystemInfo.isLinux)
     {
-      if (jalview.bin.Cache.groovyJarsPresent())
+      try
       {
-        System.out.println("Executing script " + groovyscript);
-        executeGroovyScript(groovyscript, new Object[]
-        { desktop, startUpAlframe });
-      }
-      else
+        UIManager.setLookAndFeel("com.formdev.flatlaf.FlatLightLaf");
+        set = true;
+        Console.debug("Using FlatLightLaf");
+      } catch (ClassNotFoundException | InstantiationException
+              | IllegalAccessException | UnsupportedLookAndFeelException e)
       {
-        System.err
-                .println("Sorry. Groovy Support is not available, so ignoring the provided groovy script "
-                        + groovyscript);
+        Console.debug("Exception loading FlatLightLaf", e);
       }
+      // enable custom window decorations
+      JFrame.setDefaultLookAndFeelDecorated(true);
+      JDialog.setDefaultLookAndFeelDecorated(true);
+      SwingUtilities.invokeLater(() -> {
+        FlatLightLaf.setup();
+      });
+      Console.debug("Using FlatLightLaf");
+      set = true;
     }
-    // and finally, turn off batch mode indicator - if the desktop still exists
-    if (desktop != null)
+
+    if (!set)
     {
-      if (progress != -1)
+      try
       {
-        desktop.setProgressBar(null, progress);
+        UIManager.setLookAndFeel("com.formdev.flatlaf.FlatLightLaf");
+        set = true;
+        Console.debug("Using FlatLightLaf");
+      } catch (ClassNotFoundException | InstantiationException
+              | IllegalAccessException | UnsupportedLookAndFeelException e)
+      {
+        Console.debug("Exception loading FlatLightLaf", e);
       }
-      desktop.setInBatchMode(false);
     }
+
+    if (set)
+    {
+      UIManager.put("TabbedPane.tabType", "card");
+      UIManager.put("TabbedPane.showTabSeparators", true);
+      UIManager.put("TabbedPane.showContentSeparator", true);
+      // UIManager.put("TabbedPane.tabSeparatorsFullHeight", true);
+      UIManager.put("TabbedPane.tabsOverlapBorder", true);
+      UIManager.put("TabbedPane.hasFullBorder", true);
+      UIManager.put("TabbedPane.tabLayoutPolicy", "scroll");
+      UIManager.put("TabbedPane.scrollButtonsPolicy", "asNeeded");
+      UIManager.put("TabbedPane.smoothScrolling", true);
+      UIManager.put("TabbedPane.tabWidthMode", "compact");
+      UIManager.put("TabbedPane.selectedBackground", Color.white);
+      UIManager.put("TabbedPane.background", new Color(236, 236, 236));
+      UIManager.put("TabbedPane.hoverColor", Color.lightGray);
+    }
+
+    Desktop.setLiveDragMode(Cache.getDefault("FLAT_LIVE_DRAG_MODE", true));
+    return set;
+  }
+
+  private static boolean setMacLookAndFeel()
+  {
+    boolean set = false;
+    System.setProperty("com.apple.mrj.application.apple.menu.about.name",
+            ChannelProperties.getProperty("app_name"));
+    System.setProperty("apple.laf.useScreenMenuBar", "true");
+    /*
+     * broken native LAFs on (ARM?) macbooks
+    set = setQuaquaLookAndFeel();
+    if ((!set) || !UIManager.getLookAndFeel().getClass().toString()
+            .toLowerCase(Locale.ROOT).contains("quaqua"))
+    {
+      set = setVaquaLookAndFeel();
+    }
+     */
+    set = setFlatLookAndFeel();
+    return set;
+  }
+
+  private static boolean setLinuxLookAndFeel()
+  {
+    boolean set = false;
+    set = setFlatLookAndFeel();
+    if (!set)
+      set = setMetalLookAndFeel();
+    // avoid GtkLookAndFeel -- not good results especially on HiDPI
+    if (!set)
+      set = setNimbusLookAndFeel();
+    return set;
+  }
+
+  /*
+  private static void showUsage()
+  {
+    jalview.bin.Console.outPrintln(
+            "Usage: jalview -open [FILE] [OUTPUT_FORMAT] [OUTPUT_FILE]\n\n"
+                    + "-nodisplay\tRun Jalview without User Interface.\n"
+                    + "-props FILE\tUse the given Jalview properties file instead of users default.\n"
+                    + "-colour COLOURSCHEME\tThe colourscheme to be applied to the alignment\n"
+                    + "-annotations FILE\tAdd precalculated annotations to the alignment.\n"
+                    + "-tree FILE\tLoad the given newick format tree file onto the alignment\n"
+                    + "-features FILE\tUse the given file to mark features on the alignment.\n"
+                    + "-fasta FILE\tCreate alignment file FILE in Fasta format.\n"
+                    + "-clustal FILE\tCreate alignment file FILE in Clustal format.\n"
+                    + "-pfam FILE\tCreate alignment file FILE in PFAM format.\n"
+                    + "-msf FILE\tCreate alignment file FILE in MSF format.\n"
+                    + "-pileup FILE\tCreate alignment file FILE in Pileup format\n"
+                    + "-pir FILE\tCreate alignment file FILE in PIR format.\n"
+                    + "-blc FILE\tCreate alignment file FILE in BLC format.\n"
+                    + "-json FILE\tCreate alignment file FILE in JSON format.\n"
+                    + "-jalview FILE\tCreate alignment file FILE in Jalview format.\n"
+                    + "-png FILE\tCreate PNG image FILE from alignment.\n"
+                    + "-svg FILE\tCreate SVG image FILE from alignment.\n"
+                    + "-html FILE\tCreate HTML file from alignment.\n"
+                    + "-biojsMSA FILE\tCreate BioJS MSA Viewer HTML file from alignment.\n"
+                    + "-imgMap FILE\tCreate HTML file FILE with image map of PNG image.\n"
+                    + "-eps FILE\tCreate EPS file FILE from alignment.\n"
+                    + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires.\n"
+                    + "-noquestionnaire\tTurn off questionnaire check.\n"
+                    + "-nonews\tTurn off check for Jalview news.\n"
+                    + "-nousagestats\tTurn off analytics tracking for this session.\n"
+                    + "-sortbytree OR -nosortbytree\tEnable or disable sorting of the given alignment by the given tree\n"
+                    // +
+                    // "-setprop PROPERTY=VALUE\tSet the given Jalview property,
+                    // after all other properties files have been read\n\t
+                    // (quote the 'PROPERTY=VALUE' pair to ensure spaces are
+                    // passed in correctly)"
+                    + "-jabaws URL\tSpecify URL for Jabaws services (e.g. for a local installation).\n"
+                    + "-fetchfrom nickname\tQuery nickname for features for the alignments and display them.\n"
+                    + "-groovy FILE\tExecute groovy script in FILE, after all other arguments have been processed (if FILE is the text 'STDIN' then the file will be read from STDIN)\n"
+                    + "-jvmmempc=PERCENT\tOnly available with standalone executable jar or jalview.bin.Launcher. Limit maximum heap size (memory) to PERCENT% of total physical memory detected. This defaults to 90 if total physical memory can be detected. See https://www.jalview.org/help/html/memory.html for more details.\n"
+                    + "-jvmmemmax=MAXMEMORY\tOnly available with standalone executable jar or jalview.bin.Launcher. Limit maximum heap size (memory) to MAXMEMORY. MAXMEMORY can be specified in bytes, kilobytes(k), megabytes(m), gigabytes(g) or if you're lucky enough, terabytes(t). This defaults to 32g if total physical memory can be detected, or to 8g if total physical memory cannot be detected. See https://www.jalview.org/help/html/memory.html for more details.\n"
+                    + "\n~Read documentation in Application or visit https://www.jalview.org for description of Features and Annotations file~\n\n");
   }
+  */
 
   private static void startUsageStats(final Desktop desktop)
   {
     /**
      * start a User Config prompt asking if we can log usage statistics.
      */
-    jalview.gui.PromptUserConfig prompter = new jalview.gui.PromptUserConfig(
-            desktop.desktop,
+    PromptUserConfig prompter = new PromptUserConfig(Desktop.desktop,
             "USAGESTATS",
-            "Jalview Usage Statistics",
-            "Do you want to help make Jalview better by enabling "
-                    + "the collection of usage statistics with Google Analytics ?"
-                    + "\n\n(you can enable or disable usage tracking in the preferences)",
-            new Runnable()
+            MessageManager.getString("prompt.analytics_title"),
+            MessageManager.getString("prompt.analytics"), new Runnable()
             {
+              @Override
               public void run()
               {
-                Cache.log
-                        .info("Initialising googletracker for usage stats.");
-                Cache.initGoogleTracker();
-                Cache.log.debug("Tracking enabled.");
+                Console.debug("Initialising analytics for usage stats.");
+                Cache.initAnalytics();
+                Console.debug("Tracking enabled.");
               }
             }, new Runnable()
             {
+              @Override
               public void run()
               {
-                Cache.log.info("Not enabling Google Tracking.");
+                Console.debug("Not enabling analytics.");
               }
             }, null, true);
-    SwingUtilities.invokeLater(prompter);
+    desktop.addDialogThread(prompter);
   }
 
   /**
@@ -630,14 +1660,8 @@ public class Jalview
    *          the Jalview Desktop object passed in to the groovy binding as the
    *          'Jalview' object.
    */
-  private static void executeGroovyScript(String groovyscript,
-          Object[] jalviewContext)
+  protected void executeGroovyScript(String groovyscript, AlignFrame af)
   {
-    if (jalviewContext == null)
-    {
-      System.err
-              .println("Sorry. Groovy support is currently only available when running with the Jalview GUI enabled.");
-    }
     /**
      * for scripts contained in files
      */
@@ -652,10 +1676,10 @@ public class Jalview
       try
       {
         tfile = File.createTempFile("jalview", "groovy");
-        PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
-                new FileOutputStream(tfile)));
+        PrintWriter outfile = new PrintWriter(
+                new OutputStreamWriter(new FileOutputStream(tfile)));
         BufferedReader br = new BufferedReader(
-                new java.io.InputStreamReader(System.in));
+                new InputStreamReader(System.in));
         String line = null;
         while ((line = br.readLine()) != null)
         {
@@ -667,9 +1691,10 @@ public class Jalview
 
       } catch (Exception ex)
       {
-        System.err.println("Failed to read from STDIN into tempfile "
-                + ((tfile == null) ? "(tempfile wasn't created)" : tfile
-                        .toString()));
+        jalview.bin.Console
+                .errPrintln("Failed to read from STDIN into tempfile "
+                        + ((tfile == null) ? "(tempfile wasn't created)"
+                                : tfile.toString()));
         ex.printStackTrace();
         return;
       }
@@ -678,8 +1703,8 @@ public class Jalview
         sfile = tfile.toURI().toURL();
       } catch (Exception x)
       {
-        System.err
-                .println("Unexpected Malformed URL Exception for temporary file created from STDIN: "
+        jalview.bin.Console.errPrintln(
+                "Unexpected Malformed URL Exception for temporary file created from STDIN: "
                         + tfile.toURI());
         x.printStackTrace();
         return;
@@ -695,17 +1720,20 @@ public class Jalview
         tfile = new File(groovyscript);
         if (!tfile.exists())
         {
-          System.err.println("File '" + groovyscript + "' does not exist.");
+          jalview.bin.Console.errPrintln(
+                  "File '" + groovyscript + "' does not exist.");
           return;
         }
         if (!tfile.canRead())
         {
-          System.err.println("File '" + groovyscript + "' cannot be read.");
+          jalview.bin.Console.errPrintln(
+                  "File '" + groovyscript + "' cannot be read.");
           return;
         }
         if (tfile.length() < 1)
         {
-          System.err.println("File '" + groovyscript + "' is empty.");
+          jalview.bin.Console
+                  .errPrintln("File '" + groovyscript + "' is empty.");
           return;
         }
         try
@@ -713,330 +1741,352 @@ public class Jalview
           sfile = tfile.getAbsoluteFile().toURI().toURL();
         } catch (Exception ex)
         {
-          System.err.println("Failed to create a file URL for "
+          jalview.bin.Console.errPrintln("Failed to create a file URL for "
                   + tfile.getAbsoluteFile());
           return;
         }
       }
     }
-    boolean success = false;
     try
     {
-      /*
-       * The following code performs the GroovyScriptEngine invocation using
-       * reflection, and is equivalent to this fragment from the embedding
-       * groovy documentation on the groovy site: <code> import
-       * groovy.lang.Binding; import groovy.util.GroovyScriptEngine;
-       * 
-       * String[] roots = new String[] { "/my/groovy/script/path" };
-       * GroovyScriptEngine gse = new GroovyScriptEngine(roots); Binding binding
-       * = new Binding(); binding.setVariable("input", "world");
-       * gse.run("hello.groovy", binding); </code>
-       */
-      Class[] bspec;
-      Object[] binding;
-      int blen = ((jalviewContext[0] == null) ? 0 : 1)
-              + ((jalviewContext[1] == null) ? 0 : 1);
-      String cnames[] = new String[]
-      { "Jalview", "currentAlFrame" };
-      bspec = new Class[blen * 2];
-      binding = new Object[blen * 2];
-      blen = 0;
-      ClassLoader cl = null;
-      Map vbinding = new Hashtable();
-      for (int jc = 0; jc < jalviewContext.length; jc++)
-      {
-        if (jalviewContext[jc] != null)
-        {
-          if (cl == null)
-          {
-            cl = jalviewContext[jc].getClass().getClassLoader();
-          }
-          bspec[blen * 2] = String.class;
-          bspec[blen * 2 + 1] = Object.class;
-          binding[blen * 2] = cnames[jc];
-          binding[blen * 2 + 1] = jalviewContext[jc];
-          vbinding.put(cnames[jc], jalviewContext[jc]);
-          blen++;
-        }
+      Map<String, java.lang.Object> vbinding = new HashMap<>();
+      vbinding.put("Jalview", this);
+      if (af != null)
+      {
+        vbinding.put("currentAlFrame", af);
       }
-      Class gbindingc = cl.loadClass("groovy.lang.Binding");
-      Constructor gbcons;
-      Object gbinding;
-      try
+      Binding gbinding = new Binding(vbinding);
+      GroovyScriptEngine gse = new GroovyScriptEngine(new URL[] { sfile });
+      gse.run(sfile.toString(), gbinding);
+      if ("STDIN".equals(groovyscript))
       {
-        gbcons = gbindingc.getConstructor(Map.class);
-        gbinding = gbcons.newInstance(vbinding);
-      } catch (NoSuchMethodException x)
-      {
-        // old style binding config - using series of string/object values to
-        // setVariable.
-        gbcons = gbindingc.getConstructor(null);
-        gbinding = gbcons.newInstance(null);
-        java.lang.reflect.Method setvar = gbindingc.getMethod(
-                "setVariable", bspec);
-        setvar.invoke(gbinding, binding);
+        // delete temp file that we made -
+        // only if it was successfully executed
+        tfile.delete();
       }
-      ;
-      Class gsec = cl.loadClass("groovy.util.GroovyScriptEngine");
-      Constructor gseccons = gsec.getConstructor(new Class[]
-      { URL[].class }); // String[].class });
-      Object gse = gseccons.newInstance(new Object[]
-      { new URL[]
-      { sfile } }); // .toString() } });
-      java.lang.reflect.Method run = gsec.getMethod("run", new Class[]
-      { String.class, gbindingc });
-      run.invoke(gse, new Object[]
-      { sfile.toString(), gbinding });
-      success = true;
     } catch (Exception e)
     {
-      System.err.println("Exception Whilst trying to execute file " + sfile
-              + " as a groovy script.");
+      jalview.bin.Console
+              .errPrintln("Exception Whilst trying to execute file " + sfile
+                      + " as a groovy script.");
       e.printStackTrace(System.err);
 
     }
-    if (success && groovyscript.equals("STDIN"))
+  }
+
+  public static boolean isHeadlessMode()
+  {
+    String isheadless = System.getProperty("java.awt.headless");
+    if (isheadless != null && isheadless.equalsIgnoreCase("true"))
     {
-      // delete temp file that we made - but only if it was successfully
-      // executed
-      tfile.delete();
+      return true;
     }
+    return false;
+  }
+
+  public AlignFrame[] getAlignFrames()
+  {
+    return desktop == null ? new AlignFrame[] { getCurrentAlignFrame() }
+            : Desktop.getAlignFrames();
+
   }
 
   /**
-   * Check commandline for any das server definitions or any fetchfrom switches
-   * 
-   * @return vector of DAS source nicknames to retrieve from
+   * jalview.bin.Jalview.quit() will just run the non-GUI shutdownHook and exit
    */
-  private static Vector checkDasArguments(ArgsParser aparser)
+  public void quit()
   {
-    Vector source = null;
-    String data;
-    String locsources = Cache.getProperty(Cache.DAS_LOCAL_SOURCE);
-    while ((data = aparser.getValue("dasserver", true)) != null)
-    {
-      String nickname = null;
-      String url = null;
-      boolean seq = false, feat = true;
-      int pos = data.indexOf('=');
-      // determine capabilities
-      if (pos > 0)
-      {
-        nickname = data.substring(0, pos);
-      }
-      url = data.substring(pos + 1);
-      if (url != null
-              && (url.startsWith("http:") || url
-                      .startsWith("sequence:http:")))
+    // System.exit will run the shutdownHook first
+    Jalview.exit("Quitting now. Bye!", ExitCode.OK);
+  }
+
+  public static AlignFrame getCurrentAlignFrame()
+  {
+    return Jalview.currentAlignFrame;
+  }
+
+  public static void setCurrentAlignFrame(AlignFrame currentAlignFrame)
+  {
+    Jalview.currentAlignFrame = currentAlignFrame;
+  }
+
+  public Commands getCommands()
+  {
+    return cmds;
+  }
+
+  public static void exit(String message, ExitCode ec)
+  {
+    int exitcode = ec == ExitCode.OK ? 0 : ec.ordinal() + 1;
+    if (Console.log == null)
+    {
+      // Don't start the logger just to exit!
+      if (message != null)
       {
-        if (nickname == null)
+        if (exitcode == 0)
         {
-          nickname = url;
+          Console.outPrintln(message);
         }
-        if (locsources == null)
+        else
         {
-          locsources = "";
+          jalview.bin.Console.errPrintln(message);
         }
-        else
+      }
+    }
+    else
+    {
+      Console.debug("Using Jalview.exit");
+      if (message != null)
+      {
+        if (exitcode == 0)
         {
-          locsources += "\t";
+          Console.info(message);
         }
-        locsources = locsources + nickname + "|" + url;
-        System.err
-                .println("NOTE! dasserver parameter not yet really supported (got args of "
-                        + nickname + "|" + url);
-        if (source == null)
+        else
         {
-          source = new Vector();
+          Console.error(message);
         }
-        source.addElement(nickname);
       }
-    } // loop until no more server entries are found.
-    if (locsources != null && locsources.indexOf('|') > -1)
-    {
-      Cache.log.debug("Setting local source list in properties file to:\n"
-              + locsources);
-      Cache.setProperty(Cache.DAS_LOCAL_SOURCE, locsources);
     }
-    while ((data = aparser.getValue("fetchfrom", true)) != null)
+    if (exitcode > -1)
     {
-      System.out.println("adding source '" + data + "'");
-      if (source == null)
-      {
-        source = new Vector();
-      }
-      source.addElement(data);
+      System.exit(exitcode);
     }
-    return source;
   }
 
+  public enum ExitCode
+  {
+    // only add new ones to the end of the list (to preserve ordinal values)
+    OK, FILE_NOT_FOUND, FILE_NOT_READABLE, NO_FILES, INVALID_FORMAT,
+    INVALID_ARGUMENT, INVALID_VALUE, MIXED_CLI_ARGUMENTS,
+    ERROR_RUNNING_COMMANDS, NO_LOGGING;
+  }
+
+  /******************************
+   * 
+   * TEST OUTPUT METHODS
+   * 
+   * these operate only when Arg.TESTOUTPUT has been passed, and variously check
+   * if an expected value / arg was set and report it to the test framework.
+   * 
+   ******************************/
   /**
-   * start a feature fetcher for every alignment frame
+   * report string values parsed/processed during tests When the Bootstrap
+   * argument Arg.TESTOUTPUT is present - reports on debug if given s1 is not
+   * null and not equals s2, warns if given argument is not set, and calls
+   * testoutput(true,a,s1,s2) to report processing progress.
    * 
-   * @param dasSources
+   * @param ap
+   *          - ArgParser handling parsing
+   * @param a
+   *          - Arg currently being processed
+   * @param s1
+   *          - expected
+   * @param s2
    */
-  private static FeatureFetcher startFeatureFetching(final Vector dasSources)
+  protected static void testoutput(ArgParser ap, Arg a, String s1,
+          String s2)
   {
-    FeatureFetcher ff = new FeatureFetcher();
-    AlignFrame afs[] = Desktop.getAlignframes();
-    if (afs == null || afs.length == 0)
+    BootstrapArgs bsa = ap.getBootstrapArgs();
+    if (!bsa.getBoolean(Arg.TESTOUTPUT))
+      return;
+    if (!((s1 == null && s2 == null) || (s1 != null && s1.equals(s2))))
     {
-      return null;
+      Console.debug("testoutput with unmatching values '" + s1 + "' and '"
+              + s2 + "' for arg " + a.argString());
+      return;
     }
-    for (int i = 0; i < afs.length; i++)
+    boolean isset = a.hasOption(Opt.BOOTSTRAP) ? bsa.contains(a)
+            : ap.isSet(a);
+    if (!isset)
     {
-      ff.addFetcher(afs[i], dasSources);
+      Console.warn("Arg '" + a.getName() + "' not set at all");
+      return;
     }
-    return ff;
+    testoutput(true, a, s1, s2);
   }
-}
 
-/**
- * Notes: this argParser does not distinguish between parameter switches,
- * parameter values and argument text. If an argument happens to be identical to
- * a parameter, it will be taken as such (even though it didn't have a '-'
- * prefixing it).
- * 
- * @author Andrew Waterhouse and JBP documented.
- * 
- */
-class ArgsParser
-{
-  Vector vargs = null;
+  /**
+   * report values passed via bootstrap arguments
+   * 
+   * TODO: significant code duplication with testouput(Argparser...) - move it
+   */
 
-  public ArgsParser(String[] args)
+  protected static void testoutput(BootstrapArgs bsa, Arg a, String s1,
+          String s2)
   {
-    vargs = new Vector();
-    for (int i = 0; i < args.length; i++)
+    if (!bsa.getBoolean(Arg.TESTOUTPUT))
+      return;
+    if (!((s1 == null && s2 == null) || (s1 != null && s1.equals(s2))))
     {
-      String arg = args[i].trim();
-      if (arg.charAt(0) == '-')
-      {
-        arg = arg.substring(1);
-      }
-      vargs.addElement(arg);
+      Console.debug("testoutput with unmatching values '" + s1 + "' and '"
+              + s2 + "' for arg " + a.argString());
+      return;
     }
+    if (!a.hasOption(Opt.BOOTSTRAP))
+    {
+      Console.error("Non-bootstrap Arg '" + a.getName()
+              + "' given to testoutput(BootstrapArgs bsa, Arg a, String s1, String s2) with only BootstrapArgs");
+    }
+    if (!bsa.contains(a))
+    {
+      Console.warn("Arg '" + a.getName() + "' not set at all");
+      return;
+    }
+    testoutput(true, a, s1, s2);
   }
 
   /**
-   * check for and remove first occurence of arg+parameter in arglist.
-   * 
-   * @param arg
-   * @return return the argument following the given arg if arg was in list.
+   * conditionally (on @param yes) report that expected value s1 was set during
+   * CommandsTest tests
    */
-  public String getValue(String arg)
+  private static void testoutput(boolean yes, Arg a, String s1, String s2)
   {
-    return getValue(arg, false);
+    if (yes && ((s1 == null && s2 == null)
+            || (s1 != null && s1.equals(s2))))
+    {
+      Console.outPrintln("[TESTOUTPUT] arg " + a.argString() + "='" + s1
+              + "' was set");
+    }
   }
 
-  public String getValue(String arg, boolean utf8decode)
+  /*
+   * testoutput for boolean and unary values
+   */
+  protected static void testoutput(ArgParser ap, Arg a)
   {
-    int index = vargs.indexOf(arg);
-    String dc = null, ret = null;
-    if (index != -1)
+    if (ap == null)
+      return;
+    BootstrapArgs bsa = ap.getBootstrapArgs();
+    if (bsa == null)
+      return;
+    if (!bsa.getBoolean(Arg.TESTOUTPUT))
+      return;
+    boolean val = a.hasOption(Opt.BOOTSTRAP) ? bsa.getBoolean(a)
+            : ap.getBoolean(a);
+    boolean isset = a.hasOption(Opt.BOOTSTRAP) ? bsa.contains(a)
+            : ap.isSet(a);
+    if (!isset)
     {
-      ret = vargs.elementAt(index + 1).toString();
-      vargs.removeElementAt(index);
-      vargs.removeElementAt(index);
-      if (utf8decode && ret != null)
-      {
-        try
-        {
-          dc = URLDecoder.decode(ret, "UTF-8");
-          ret = dc;
-        } catch (Exception e)
-        {
-          // TODO: log failure to decode
-        }
-      }
+      Console.warn("Arg '" + a.getName() + "' not set at all");
+      return;
     }
-    return ret;
+    testoutput(val, a);
   }
 
-  /**
-   * check for and remove first occurence of arg in arglist.
-   * 
-   * @param arg
-   * @return true if arg was present in argslist.
-   */
-  public boolean contains(String arg)
+  protected static void testoutput(BootstrapArgs bsa, Arg a)
   {
-    if (vargs.contains(arg))
+    if (!bsa.getBoolean(Arg.TESTOUTPUT))
+      return;
+    if (!a.hasOption(Opt.BOOTSTRAP))
     {
-      vargs.removeElement(arg);
-      return true;
+      Console.warn("Non-bootstrap Arg '" + a.getName()
+              + "' given to testoutput(BootstrapArgs bsa, Arg a) with only BootstrapArgs");
+
     }
-    else
+    if (!bsa.contains(a))
+    {
+      Console.warn("Arg '" + a.getName() + "' not set at all");
+      return;
+    }
+    testoutput(bsa.getBoolean(a), a);
+  }
+
+  private static void testoutput(boolean yes, Arg a)
+  {
+    String message = null;
+    if (a.hasOption(Opt.BOOLEAN))
+    {
+      message = (yes ? a.argString() : a.negateArgString()) + " was set";
+    }
+    else if (a.hasOption(Opt.UNARY))
     {
-      return false;
+      message = a.argString() + (yes ? " was set" : " was not set");
     }
+    Console.outPrintln("[TESTOUTPUT] arg " + message);
   }
 
-  public String nextValue()
+  public ArgParser getArgParser()
   {
-    return vargs.remove(0).toString();
+    return argparser;
   }
 
-  public int getSize()
+  public BootstrapArgs getBootstrapArgs()
   {
-    return vargs.size();
+    return bootstrapArgs;
   }
 
-}
+  public static boolean isBatchMode()
+  {
+    return getInstance() != null && (getInstance().desktop == null
+            || getInstance().desktop.isInBatchMode());
+  }
 
-/**
- * keep track of feature fetching tasks.
- * 
- * @author JimP
- * 
- */
-class FeatureFetcher
-{
-  /*
-   * TODO: generalise to track all jalview events to orchestrate batch
-   * processing events.
+  /**
+   * Warning about old or mixed command line arguments
    */
-
-  private int queued = 0;
-
-  private int running = 0;
-
-  public FeatureFetcher()
+  private void mixedCliWarning()
   {
+    Jalview j = Jalview.getInstance();
+    boolean mixedStyle = j.getArgParser() != null
+            && j.getArgParser().isMixedStyle();
+    String title = MessageManager.getString("label.command_line_arguments");
+    if (mixedStyle)
+    {
+      String warning = MessageManager.formatMessage(
+              "warning.using_mixed_command_line_arguments",
+              j.getArgParser().getMixedExamples());
+      String quit = MessageManager.getString("action.quit");
 
+      Desktop.instance.nonBlockingDialog(title, warning, null, quit,
+              JvOptionPane.WARNING_MESSAGE, false, false, true, 30000);
+
+      Jalview.exit(
+              "Exiting due to mixed old and new command line arguments.",
+              ExitCode.MIXED_CLI_ARGUMENTS);
+    }
   }
 
-  public void addFetcher(final AlignFrame af, final Vector dasSources)
+  private void cliWarning()
   {
-    final long id = System.currentTimeMillis();
-    queued++;
-    final FeatureFetcher us = this;
-    new Thread(new Runnable()
+    Jalview j = Jalview.getInstance();
+    Commands c = j.getCommands();
+    boolean oldStyle = j.getArgParser() != null
+            && j.getArgParser().isOldStyle();
+    String title = MessageManager.getString("label.command_line_arguments");
+    if (oldStyle)
     {
-
-      public void run()
+      String warning = MessageManager
+              .getString("warning.using_old_command_line_arguments");
+      String url = "<a href=\"https://www.jalview.org/help/html/features/commandline.html\">https://www.jalview.org/help/html/features/commandline.html</a>";
+      if (Desktop.instance != null)
       {
-        synchronized (us)
-        {
-          queued--;
-          running++;
-        }
+        String cont = MessageManager.getString("label.continue");
 
-        af.setProgressBar("DAS features being retrieved...", id);
-        af.featureSettings_actionPerformed(null);
-        af.featureSettings.fetchDasFeatures(dasSources, true);
-        af.setProgressBar(null, id);
-        synchronized (us)
+        Desktop.instance.nonBlockingDialog(title, warning, url, cont,
+                JvOptionPane.WARNING_MESSAGE, false, true, true, 30000);
+      }
+    }
+    if (j.getCommands() != null && j.getCommands().getErrors().size() > 0)
+    {
+      if (Desktop.instance != null)
+      {
+        String message = MessageManager
+                .getString("warning.the_following_errors");
+        String ok = MessageManager.getString("action.ok");
+        int shortest = 60;
+        List<String> errors = j.getCommands().getErrors();
+        for (int i = 0; i < errors.size(); i++)
         {
-          running--;
+          shortest = Math.min(shortest, errors.get(i).length());
         }
+        Desktop.instance.nonBlockingDialog(
+                Math.max(message.length(), Math.min(60, shortest)),
+                Math.min(errors.size(), 20), title, message,
+                j.getCommands().errorsToString(), ok,
+                JvOptionPane.WARNING_MESSAGE, true, false, true, -1);
       }
-    }).start();
+    }
   }
 
-  public synchronized boolean allFinished()
-  {
-    return queued == 0 && running == 0;
-  }
-};
+}