3 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintStream;
9 import io.github.classgraph.ClassGraph;
10 import io.github.classgraph.ScanResult;
14 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
15 * -using-javas-runtime-exec
20 public class Worker extends Thread
22 final Process process;
24 private BufferedReader outputReader;
26 private BufferedReader errorReader;
30 public Worker(Process process)
32 this.process = process;
40 exit = process.waitFor();
41 } catch (InterruptedException ignore)
47 public BufferedReader getOutputReader()
52 public void setOutputReader(BufferedReader outputReader)
54 this.outputReader = outputReader;
57 public BufferedReader getErrorReader()
62 public void setErrorReader(BufferedReader errorReader)
64 this.errorReader = errorReader;
69 * @return null or process exit code
71 public Integer getExitValue()
77 * terminate with prejudice
79 public void destroyProcess()
82 currentThread().interrupt();
86 private static ClassGraph scanner = null;
88 private static String classpath = null;
90 public synchronized static String getClassPath()
94 scanner = new ClassGraph();
95 ScanResult scan = scanner.scan();
96 classpath = scan.getClasspath();
98 while (classpath == null)
103 } catch (InterruptedException x)
111 public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
114 return jalviewDesktopRunner(withAwt, cmd, timeout, "");
117 public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
118 int timeout, String javaArgs)
120 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
121 // not expanded by the shell
122 String classpath = getClassPath();
123 String javahome = System.getProperty("java.home");
125 String _cmd = javahome + File.pathSeparator + "java "
126 + (withAwt ? "-Djava.awt.headless=true" : "")
127 + " -classpath " + classpath + " " + javaArgs
128 + " jalview.bin.Jalview ";
129 Process ls2_proc = null;
130 Worker worker = null;
133 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
134 } catch (IOException e1)
136 e1.printStackTrace();
138 if (ls2_proc != null)
140 BufferedReader outputReader = new BufferedReader(
141 new InputStreamReader(ls2_proc.getInputStream()));
142 BufferedReader errorReader = new BufferedReader(
143 new InputStreamReader(ls2_proc.getErrorStream()));
144 worker = new Worker(ls2_proc);
148 worker.join(timeout);
149 } catch (InterruptedException e)
151 // e.printStackTrace();
153 worker.setOutputReader(outputReader);
154 worker.setErrorReader(errorReader);
161 public Echo(final PrintStream out, final BufferedReader source)
165 new Thread(new Runnable()
173 while ((line = source.readLine()) != null)
177 } catch (IOException q)
183 } catch (Exception ex)
194 new Echo(System.out, getOutputReader());
195 new Echo(System.err, getErrorReader());