21 package org.matsim.run.gui;
23 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.util.Arrays;
29 import javax.swing.JTextArea;
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
43 final static Logger log = LogManager.getLogger(ExeRunner.class);
45 private final ExternalExecutor executor;
47 public static ExeRunner run(
final String[] cmdArgs,
final JTextArea stdOut,
final JTextArea errOut,
final String workingDirectory) {
48 final ExternalExecutor myExecutor =
new ExternalExecutor(cmdArgs, stdOut, errOut, workingDirectory);
49 ExeRunner runner =
new ExeRunner(myExecutor);
54 private ExeRunner(ExternalExecutor executor) {
55 this.executor = executor;
58 public void killProcess() {
59 this.executor.killProcess();
62 public int waitForFinish() {
63 synchronized (this.executor) {
66 }
catch (InterruptedException e) {
67 log.info(
"Got interrupted while waiting for external exe to finish.", e);
71 return this.executor.erg;
75 final String[] cmdArgs;
76 final JTextArea stdOut;
77 final JTextArea errOut;
78 final String workingDirectory;
79 private Process
p = null;
83 public ExternalExecutor (
final String[] cmdArgs,
final JTextArea stdOut,
final JTextArea errOut,
final String workingDirectory) {
84 this.cmdArgs = cmdArgs;
87 this.workingDirectory = workingDirectory;
98 var processBuilder =
new ProcessBuilder();
99 processBuilder.environment().put(
"MATSIM_GUI",
"true");
103 if (System.getProperty(
"MATSIM_GUI_ARGS") != null) {
104 processBuilder.environment().put(
"MATSIM_GUI_ARGS", System.getProperty(
"MATSIM_GUI_ARGS"));
107 if (workingDirectory != null) {
108 processBuilder.directory(
new File(workingDirectory));
110 processBuilder.command(cmdArgs);
113 this.p = processBuilder.start();
115 BufferedReader in =
new BufferedReader(
new InputStreamReader(this.p.getInputStream()));
116 BufferedReader err =
new BufferedReader(
new InputStreamReader(this.p.getErrorStream()));
118 StreamHandler outputHandler =
new StreamHandler(in, this.stdOut);
119 outputHandler.start();
121 StreamHandler errorHandler =
new StreamHandler(err, this.stdOut, this.errOut);
122 errorHandler.start();
124 log.info(
"Starting external exe with command: " + Arrays.toString(
this.cmdArgs));
125 boolean processRunning =
true;
126 while (processRunning) {
129 this.erg = this.p.exitValue();
130 log.info(
"external exe returned " + this.erg);
131 processRunning =
false;
132 }
catch (InterruptedException e) {
133 log.info(
"Thread waiting for external exe to finish was interrupted");
138 outputHandler.join();
139 }
catch (InterruptedException e) {
140 log.info(
"got interrupted while waiting for outputHandler to die.", e);
144 }
catch (InterruptedException e) {
145 log.info(
"got interrupted while waiting for errorHandler to die.", e);
147 }
catch (IOException e) {
148 log.error(
"problem running executable.", e);
154 static class StreamHandler
extends Thread {
155 private final BufferedReader in;
156 private final JTextArea[] textArea;
158 public StreamHandler(
final BufferedReader in,
final JTextArea... textArea) {
160 this.textArea = textArea;
167 while ((line = this.in.readLine()) != null) {
168 for (JTextArea out : this.textArea) {
171 int length = out.getDocument().getLength();
172 out.setCaretPosition(length);
174 if (length > 512*1024) {
175 out.setText(out.getText().substring(256*1024));
179 }
catch (IOException e) {
180 log.info(
"StreamHandler got interrupted", e);
ExternalExecutor(final String[] cmdArgs, final JTextArea stdOut, final JTextArea errOut, final String workingDirectory)
static final String NATIVE_NEWLINE