comparison test/org/tmatesoft/hg/test/ExecHelper.java @ 110:0170f95ca915

On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 31 Jan 2011 19:42:19 +0100
parents a3a2e5deb320
children 32e794c599d7
comparison
equal deleted inserted replaced
109:dd4d2d0e42cd 110:0170f95ca915
18 18
19 import java.io.File; 19 import java.io.File;
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.io.InputStreamReader; 21 import java.io.InputStreamReader;
22 import java.nio.CharBuffer; 22 import java.nio.CharBuffer;
23 import java.util.ArrayList;
24 import java.util.Arrays;
23 import java.util.LinkedList; 25 import java.util.LinkedList;
26 import java.util.StringTokenizer;
24 27
25 /** 28 /**
26 * 29 *
27 * @author Artem Tikhomirov 30 * @author Artem Tikhomirov
28 * @author TMate Software Ltd. 31 * @author TMate Software Ltd.
36 parser = outParser; 39 parser = outParser;
37 dir = workingDir; 40 dir = workingDir;
38 } 41 }
39 42
40 public void run(String... cmd) throws IOException, InterruptedException { 43 public void run(String... cmd) throws IOException, InterruptedException {
41 Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start(); 44 ProcessBuilder pb = null;
42 // Process p = Runtime.getRuntime().exec(cmd, null, dir); 45 if (System.getProperty("os.name").startsWith("Windows")) {
46 StringTokenizer st = new StringTokenizer(System.getenv("PATH"), ";");
47 while (st.hasMoreTokens()) {
48 File pe = new File(st.nextToken());
49 if (new File(pe, cmd[0] + ".exe").exists()) {
50 break;
51 }
52 if (new File(pe, cmd[0] + ".bat").exists() || new File(pe, cmd[0] + ".cmd").exists()) {
53 ArrayList<String> command = new ArrayList<String>();
54 command.add("cmd.exe");
55 command.add("/C");
56 command.addAll(Arrays.asList(cmd));
57 pb = new ProcessBuilder(command);
58 break;
59 }
60 }
61 }
62 if (pb == null) {
63 pb = new ProcessBuilder(cmd);
64 }
65 Process p = pb.directory(dir).redirectErrorStream(true).start();
43 InputStreamReader stdOut = new InputStreamReader(p.getInputStream()); 66 InputStreamReader stdOut = new InputStreamReader(p.getInputStream());
44 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>(); 67 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>();
45 int r = -1; 68 int r = -1;
46 CharBuffer b = null; 69 CharBuffer b = null;
47 do { 70 do {