Use args[] form of System.exec in order to resolve path to java on osx that may inclu...
[jalview.git] / src / jalview / util / Worker.java
index 68f4174..b5297e1 100644 (file)
@@ -1,9 +1,12 @@
 package jalview.util;
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Arrays;
 
 import io.github.classgraph.ClassGraph;
 import io.github.classgraph.ScanResult;
@@ -119,7 +122,23 @@ public class Worker extends Thread
     // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
     // not expanded by the shell
     String classpath = getClassPath();
-    String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
+    String javahome = System.getProperty("java.home");
+    File java = new File(javahome + File.separator + "bin"
+            + File.separator + "java");
+    if (!java.exists())
+    {
+      System.err.println("Can't find " + java.getAbsolutePath());
+      java = new File(javahome + File.separator + "bin"
+              + File.separator + "java.exe");
+      if (!java.exists())
+      {
+        System.err.println("Can't find " + java.getAbsolutePath());
+        throw new Error(
+                "Can't locate java in JAVA_HOME/bin/java or JAVA_HOME/bin/java.exe");
+      }
+    }
+    String _cmd = "\"" + javahome + "\" "
+            + (withAwt ? "-Djava.awt.headless=true" : "")
             + " -classpath " + classpath + " " + javaArgs
             + " jalview.bin.Jalview ";
     Process ls2_proc = null;
@@ -152,6 +171,69 @@ public class Worker extends Thread
     return worker;
   }
 
+  public static Worker jalviewDesktopRunner(boolean withAwt, String[] args,
+          int timeout, String[] javaArgs)
+  {
+    // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
+    // not expanded by the shell
+    String classpath = getClassPath();
+    String javahome = System.getProperty("java.home");
+    File java = new File(
+            javahome + File.separator + "bin" + File.separator + "java");
+    if (!java.exists())
+    {
+      System.err.println("Can't find " + java.getAbsolutePath());
+      java = new File(javahome + File.separator + "bin" + File.separator
+              + "java.exe");
+      if (!java.exists())
+      {
+        System.err.println("Can't find " + java.getAbsolutePath());
+        throw new Error(
+                "Can't locate java in JAVA_HOME/bin/java or JAVA_HOME/bin/java.exe");
+      }
+    }
+    ArrayList<String> _cmd = new ArrayList();
+    _cmd.add(java.getAbsolutePath());
+    if (withAwt)
+    {
+      _cmd.add("-Djava.awt.headless=true");
+    }
+    _cmd.add("-classpath");
+    _cmd.add(classpath);
+    _cmd.addAll(Arrays.asList(javaArgs));
+    _cmd.add("jalview.bin.Jalview");
+    _cmd.addAll(Arrays.asList(args));
+    Process ls2_proc = null;
+    Worker worker = null;
+    try
+    {
+      ls2_proc = Runtime.getRuntime()
+              .exec(_cmd.toArray(new String[_cmd.size()]));
+    } catch (IOException e1)
+    {
+      e1.printStackTrace();
+    }
+    if (ls2_proc != null)
+    {
+      BufferedReader outputReader = new BufferedReader(
+              new InputStreamReader(ls2_proc.getInputStream()));
+      BufferedReader errorReader = new BufferedReader(
+              new InputStreamReader(ls2_proc.getErrorStream()));
+      worker = new Worker(ls2_proc);
+      worker.start();
+      try
+      {
+        worker.join(timeout);
+      } catch (InterruptedException e)
+      {
+        // e.printStackTrace();
+      }
+      worker.setOutputReader(outputReader);
+      worker.setErrorReader(errorReader);
+    }
+    return worker;
+  }
+
   class Echo
   {
     public Echo(final PrintStream out, final BufferedReader source)