java调用bat、shell脚本通用类,工作记录。
1package test; 2 3import java.io.BufferedReader; 4import java.io.BufferedWriter; 5import java.io.IOException; 6import java.io.InputStream; 7import java.io.InputStreamReader; 8import java.io.OutputStreamWriter; 9import java.util.ArrayList; 10import java.util.Arrays; 11import java.util.List; 12import java.util.concurrent.Callable; 13import java.util.concurrent.ExecutionException; 14import java.util.concurrent.ExecutorService; 15import java.util.concurrent.Executors; 16import java.util.concurrent.FutureTask; 17 18import test.ProcessUtil.Result.Judege; 19 20public class ProcessUtil 21{ 22 23 private static final ExecutorService EXECUTORS = Executors.newFixedThreadPool(4); 24 25 public static ProcessRunner buildProcessRunner() 26 { 27 return new ProcessRunner(); 28 } 29 30 public static class ProcessRunner 31 { 32 private int maxErrorLineNumb = 100; 33 34 private int maxInputLineNumb = 500; 35 36 /** 37 * 调用核心的命令 38 * 39 * [@param](https://my.oschina.net/u/2303379) cmd 40 * 命令 41 * [@param](https://my.oschina.net/u/2303379) cmdInputParas 42 * 执行命令需要输入的参数,比如命令行登录数据库需要输入密码<br> 43 * echo "password" | cmd 44 * [@return](https://my.oschina.net/u/556800) 返回值,包括错误回显,正确回显,整个脚本执行的返回码 45 * 46 */ 47 public Result runCMD(List<String> cmd, List<String> cmdInputParas) 48 { 49 Process process = null; 50 BufferedWriter bw = null; 51 try 52 { 53 ProcessBuilder processBuilder = new ProcessBuilder(cmd); 54 process = processBuilder.start(); 55 bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); 56 for (String p : cmdInputParas) 57 { 58 bw.write(p); 59 bw.newLine(); 60 } 61 bw.flush(); 62 bw.close(); 63 Callable<List<String>> inputRunner = new Runner(maxErrorLineNumb, process.getInputStream()); 64 FutureTask<List<String>> inputTask = new FutureTask<List<String>>(inputRunner); 65 EXECUTORS.execute(inputTask); 66 Callable<List<String>> errorRunner = new Runner(maxInputLineNumb, process.getErrorStream()); 67 FutureTask<List<String>> errorTask = new FutureTask<List<String>>(errorRunner); 68 EXECUTORS.execute(errorTask); 69 List<String> inputResult = inputTask.get(); 70 List<String> errorResult = errorTask.get(); 71 int returnCode = process.waitFor(); 72 return new Result(inputResult, errorResult, returnCode); 73 74 } 75 catch (IOException e) 76 { 77 e.printStackTrace(); 78 } 79 catch (InterruptedException e) 80 { 81 e.printStackTrace(); 82 } 83 catch (ExecutionException e) 84 { 85 e.printStackTrace(); 86 } 87 finally 88 { 89 try 90 { 91 if (bw != null) 92 { 93 bw.close(); 94 } 95 } 96 catch (IOException e) 97 { 98 99 } 100 if (process != null) 101 { 102 process.destroy(); 103 } 104 105 } 106 return new Result(); 107 } 108 109 /** 110 * 调用核心的命令 111 * 112 * [@param](https://my.oschina.net/u/2303379) cmd 113 * 命令 114 * 115 * [@return](https://my.oschina.net/u/556800) 返回值,包括错误回显,正确回显,整个脚本执行的返回码 116 */ 117 public Result runCMD(List<String> cmd) 118 { 119 return runCMD(cmd, Arrays.asList()); 120 } 121 122 } 123 124 /** 125 * 执行命令类 126 * 127 */ 128 private static class Runner implements Callable<List<String>> 129 { 130 131 private int maxLineNumb; 132 133 private InputStream inputStream; 134 135 public Runner(int maxLineNumb, InputStream inputStream) 136 { 137 super(); 138 this.maxLineNumb = maxLineNumb; 139 this.inputStream = inputStream; 140 } 141 142 @Override 143 public List<String> call() 144 { 145 List<String> result = new ArrayList<String>(); 146 BufferedReader br = null; 147 try 148 { 149 br = new BufferedReader(new InputStreamReader(inputStream)); 150 int i = 0; 151 String line = null; 152 while (null != (line = br.readLine()) && (i++ < maxLineNumb)) 153 { 154 result.add(line); 155 } 156 return result; 157 } 158 catch (IOException e) 159 { 160 161 e.printStackTrace(); 162 } 163 finally 164 { 165 try 166 { 167 if (br != null) 168 { 169 br.close(); 170 } 171 } 172 catch (IOException e) 173 { 174 175 } 176 } 177 return result; 178 } 179 180 } 181 182 /** 183 * 结果 184 * 185 */ 186 public static class Result 187 { 188 // 回显 189 private List<String> inputInfos; 190 // 错误回显 191 private List<String> errorInfos; 192 // 返回码 193 private int returnCode = -1; 194 195 public Result(List<String> inputInfos, List<String> errorInfos, int returnCode) 196 { 197 this.inputInfos = inputInfos; 198 this.errorInfos = errorInfos; 199 this.returnCode = returnCode; 200 } 201 202 public Result() 203 { 204 205 } 206 207 @Override 208 public String toString() 209 { 210 return "Result [inputInfos=" + inputInfos + ", errorInfos=" + errorInfos + ", returnCode=" + returnCode + "]"; 211 } 212 213 public boolean isSuccess(Judege j) 214 { 215 if (errorInfos == null || inputInfos == null || returnCode == -1) 216 { 217 return false; 218 } 219 return j.isSuccess(inputInfos, errorInfos, returnCode); 220 } 221 222 /** 223 * 判断结果接口可以通用化判断,防止组内人胡编乱写 224 */ 225 interface Judege 226 { 227 public boolean isSuccess(List<String> inputInfos, List<String> errorInfos, int returnCode); 228 } 229 } 230 231 public static void main(String[] args) 232 { 233 ProcessRunner runner = ProcessUtil.buildProcessRunner(); 234 Result result = runner.runCMD(Arrays.asList("cmd", "/C", "ping 127.0.0.1 -n 1")); 235 System.out.println(result); 236 boolean isRight = result.isSuccess(new Judege() 237 { 238 @Override 239 public boolean isSuccess(List<String> inputInfos, List<String> errorInfos, int returnCode) 240 { 241 return returnCode == 0 && errorInfos.size() == 0; 242 } 243 }); 244 System.out.println("result is " + isRight); 245 } 246 247}