1919 */
2020package processing .app .exec ;
2121
22- import java .io .IOException ;
23- import java . io . StringWriter ;
22+ import java .io .* ;
23+
2424import processing .core .PApplet ;
2525
2626/**
3131 */
3232public class ProcessHelper {
3333 private final String [] cmd ;
34+ // private final String exe;
35+ // private final String[] args;
36+ private final File dir ;
37+
3438
3539 public ProcessHelper (final String ... cmd ) {
3640 this .cmd = cmd ;
41+ this .dir = null ;
42+ }
43+
44+
45+ public ProcessHelper (File dir , final String ... cmd ) {
46+ this .cmd = cmd ;
47+ this .dir = dir ;
3748 }
49+
3850
3951 @ Override
4052 public String toString () {
53+ /*
4154 final StringBuffer buffer = new StringBuffer();
4255 for (int i = 0; i < cmd.length; i++) {
4356 if (i != 0) {
@@ -46,8 +59,12 @@ public String toString() {
4659 buffer.append(cmd[i]);
4760 }
4861 return buffer.toString();
62+ */
63+ // return exe + " " + PApplet.join(args, " ");
64+ return PApplet .join (cmd , " " );
4965 }
5066
67+
5168 /**
5269 * Blocking execution.
5370 * @return exit value of process
@@ -62,10 +79,13 @@ public ProcessResult execute() throws InterruptedException, IOException {
6279 final String prettyCommand = toString ();
6380 // System.err.println("ProcessHelper: >>>>> " + Thread.currentThread().getId()
6481 // + " " + prettyCommand);
65- final Process process = Runtime .getRuntime ().exec (cmd );
82+ // final Process process = Runtime.getRuntime().exec(cmd);
83+ final Process process = dir == null ?
84+ Runtime .getRuntime ().exec (cmd ) :
85+ Runtime .getRuntime ().exec (cmd , new String [] { }, dir );
6686 ProcessRegistry .watch (process );
6787 try {
68- String title = PApplet . join ( cmd , ' ' );
88+ String title = prettyCommand ;
6989 new StreamPump (process .getInputStream (), "out: " + title ).addTarget (outWriter ).start ();
7090 new StreamPump (process .getErrorStream (), "err: " + title ).addTarget (errWriter ).start ();
7191 try {
@@ -85,4 +105,39 @@ public ProcessResult execute() throws InterruptedException, IOException {
85105 ProcessRegistry .unwatch (process );
86106 }
87107 }
108+
109+
110+ // static public ProcessResult execute(String exe, String[] args, File dir)
111+ // throws InterruptedException, IOException {
112+ // final StringWriter outWriter = new StringWriter();
113+ // final StringWriter errWriter = new StringWriter();
114+ // final long startTime = System.currentTimeMillis();
115+ //
116+ // final String prettyCommand = exe + " " + PApplet.join(args, " ");
117+ // System.out.println("pretty cmd is " + prettyCommand);
118+ // final Process process = dir == null ?
119+ // Runtime.getRuntime().exec(exe, args) :
120+ // Runtime.getRuntime().exec(exe, args, dir);
121+ // ProcessRegistry.watch(process);
122+ // try {
123+ // String title = prettyCommand;
124+ // new StreamPump(process.getInputStream(), "out: " + title).addTarget(outWriter).start();
125+ // new StreamPump(process.getErrorStream(), "err: " + title).addTarget(errWriter).start();
126+ // try {
127+ // final int result = process.waitFor();
128+ // final long time = System.currentTimeMillis() - startTime;
129+ // // System.err.println("ProcessHelper: <<<<< "
130+ // // + Thread.currentThread().getId() + " " + cmd[0] + " (" + time
131+ // // + "ms)");
132+ // return new ProcessResult(prettyCommand, result, outWriter.toString(),
133+ // errWriter.toString(), time);
134+ // } catch (final InterruptedException e) {
135+ // System.err.println("Interrupted: " + prettyCommand);
136+ // throw e;
137+ // }
138+ // } finally {
139+ // process.destroy();
140+ // ProcessRegistry.unwatch(process);
141+ // }
142+ // }
88143}
0 commit comments