Java调用bat sh脚本通用类

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}
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java基础知识随身记

2018年11月12日20:51:35一、基础知识:1、JVM、JRE和JDK的区别:JVM(JavaVirtualMachine):java虚拟机,用于保证java的跨平台的特性。  java语言是跨平台,jvm不是跨平台的。JRE(JavaRuntimeEnvironment):java的运行环境,包括jvmjava的核心类

java的linux执行的shell

!/bin/sh 该脚本为Linux下启动java程序的通用脚本。即可以作为开机自启动service脚本被调用, 也可以作为启动java程序的独立脚本来使用。   警告!!!:该脚本stop部分使用系统kill命令来强制终止指定的java程序进程。 在杀死进程前,未

Java多态实现原理

Java多态概述多态是面向对象编程语言的重要特性,它允许基类的指针或引用指向派生类的对象,而在具体访问时实现方法的动态绑定。Java对于方法调用动态绑定的实现主要依赖于方法表,但通过类引用调用(invokevirtual)和接口引用调用(invokeinterface)的实现则有所不同。类引用调用的大致过程为:Java编译器将Java源代码编译成c

Java执行shell脚本并返回结果两种方法的完整代码

Java执行shell脚本并返回结果两种方法的完整代码简单的是直接传入String字符串,这种不能执行echo或者需要调用其他进程的命令(比如调用postfix发送邮件命令就不起作用)执行复杂的shell建议使用String\\方式传递(对外可以封装后也传入String字符串)。/运行shell脚本

Shell 脚本 —— java 代码远程调用shell脚本重启 tomcat

个人博客网:https://wushaopei.github.io/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwushaopei.github.io%2F)  (你想要这里多有)1、创建maven工程!(https://oscimg.oschin