3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.PrintStream;
8 import io.github.classgraph.ClassGraph;
9 import io.github.classgraph.ScanResult;
13 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
14 * -using-javas-runtime-exec
19 public class Worker extends Thread
21 final Process process;
23 private BufferedReader outputReader;
25 private BufferedReader errorReader;
29 public Worker(Process process)
31 this.process = process;
39 exit = process.waitFor();
40 } catch (InterruptedException ignore)
46 public BufferedReader getOutputReader()
51 public void setOutputReader(BufferedReader outputReader)
53 this.outputReader = outputReader;
56 public BufferedReader getErrorReader()
61 public void setErrorReader(BufferedReader errorReader)
63 this.errorReader = errorReader;
68 * @return null or process exit code
70 public Integer getExitValue()
76 * terminate with prejudice
78 public void destroyProcess()
81 currentThread().interrupt();
85 private static ClassGraph scanner = null;
87 private static String classpath = null;
89 public synchronized static String getClassPath()
93 scanner = new ClassGraph();
94 ScanResult scan = scanner.scan();
95 classpath = scan.getClasspath();
97 while (classpath == null)
102 } catch (InterruptedException x)
110 public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
113 return jalviewDesktopRunner(withAwt, cmd, timeout, "");
116 public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
117 int timeout, String javaArgs)
119 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
120 // not expanded by the shell
121 String classpath = getClassPath();
122 String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
123 + " -classpath " + classpath + " " + javaArgs
124 + " jalview.bin.Jalview ";
125 Process ls2_proc = null;
126 Worker worker = null;
129 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
130 } catch (IOException e1)
132 e1.printStackTrace();
134 if (ls2_proc != null)
136 BufferedReader outputReader = new BufferedReader(
137 new InputStreamReader(ls2_proc.getInputStream()));
138 BufferedReader errorReader = new BufferedReader(
139 new InputStreamReader(ls2_proc.getErrorStream()));
140 worker = new Worker(ls2_proc);
144 worker.join(timeout);
145 } catch (InterruptedException e)
147 // e.printStackTrace();
149 worker.setOutputReader(outputReader);
150 worker.setErrorReader(errorReader);
157 public Echo(final PrintStream out, final BufferedReader source)
161 new Thread(new Runnable()
169 while ((line = source.readLine()) != null)
173 } catch (IOException q)
179 } catch (Exception ex)
190 new Echo(System.out, getOutputReader());
191 new Echo(System.err, getErrorReader());