java调用bat、shell脚本通用类,工作记录。
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import test.ProcessUtil.Result.Judege;
public class ProcessUtil
{
private static final ExecutorService EXECUTORS = Executors.newFixedThreadPool(4);
public static ProcessRunner buildProcessRunner()
{
return new ProcessRunner();
}
public static class ProcessRunner
{
private int maxErrorLineNumb = 100;
private int maxInputLineNumb = 500;
/**
* 调用核心的命令
*
* [@param](https://my.oschina.net/u/2303379) cmd
* 命令
* [@param](https://my.oschina.net/u/2303379) cmdInputParas
* 执行命令需要输入的参数,比如命令行登录数据库需要输入密码<br>
* echo "password" | cmd
* [@return](https://my.oschina.net/u/556800) 返回值,包括错误回显,正确回显,整个脚本执行的返回码
*
*/
public Result runCMD(List<String> cmd, List<String> cmdInputParas)
{
Process process = null;
BufferedWriter bw = null;
try
{
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
process = processBuilder.start();
bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
for (String p : cmdInputParas)
{
bw.write(p);
bw.newLine();
}
bw.flush();
bw.close();
Callable<List<String>> inputRunner = new Runner(maxErrorLineNumb, process.getInputStream());
FutureTask<List<String>> inputTask = new FutureTask<List<String>>(inputRunner);
EXECUTORS.execute(inputTask);
Callable<List<String>> errorRunner = new Runner(maxInputLineNumb, process.getErrorStream());
FutureTask<List<String>> errorTask = new FutureTask<List<String>>(errorRunner);
EXECUTORS.execute(errorTask);
List<String> inputResult = inputTask.get();
List<String> errorResult = errorTask.get();
int returnCode = process.waitFor();
return new Result(inputResult, errorResult, returnCode);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
finally
{
try
{
if (bw != null)
{
bw.close();
}
}
catch (IOException e)
{
}
if (process != null)
{
process.destroy();
}
}
return new Result();
}
/**
* 调用核心的命令
*
* [@param](https://my.oschina.net/u/2303379) cmd
* 命令
*
* [@return](https://my.oschina.net/u/556800) 返回值,包括错误回显,正确回显,整个脚本执行的返回码
*/
public Result runCMD(List<String> cmd)
{
return runCMD(cmd, Arrays.asList());
}
}
/**
* 执行命令类
*
*/
private static class Runner implements Callable<List<String>>
{
private int maxLineNumb;
private InputStream inputStream;
public Runner(int maxLineNumb, InputStream inputStream)
{
super();
this.maxLineNumb = maxLineNumb;
this.inputStream = inputStream;
}
@Override
public List<String> call()
{
List<String> result = new ArrayList<String>();
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(inputStream));
int i = 0;
String line = null;
while (null != (line = br.readLine()) && (i++ < maxLineNumb))
{
result.add(line);
}
return result;
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
{
br.close();
}
}
catch (IOException e)
{
}
}
return result;
}
}
/**
* 结果
*
*/
public static class Result
{
// 回显
private List<String> inputInfos;
// 错误回显
private List<String> errorInfos;
// 返回码
private int returnCode = -1;
public Result(List<String> inputInfos, List<String> errorInfos, int returnCode)
{
this.inputInfos = inputInfos;
this.errorInfos = errorInfos;
this.returnCode = returnCode;
}
public Result()
{
}
@Override
public String toString()
{
return "Result [inputInfos=" + inputInfos + ", errorInfos=" + errorInfos + ", returnCode=" + returnCode + "]";
}
public boolean isSuccess(Judege j)
{
if (errorInfos == null || inputInfos == null || returnCode == -1)
{
return false;
}
return j.isSuccess(inputInfos, errorInfos, returnCode);
}
/**
* 判断结果接口可以通用化判断,防止组内人胡编乱写
*/
interface Judege
{
public boolean isSuccess(List<String> inputInfos, List<String> errorInfos, int returnCode);
}
}
public static void main(String[] args)
{
ProcessRunner runner = ProcessUtil.buildProcessRunner();
Result result = runner.runCMD(Arrays.asList("cmd", "/C", "ping 127.0.0.1 -n 1"));
System.out.println(result);
boolean isRight = result.isSuccess(new Judege()
{
@Override
public boolean isSuccess(List<String> inputInfos, List<String> errorInfos, int returnCode)
{
return returnCode == 0 && errorInfos.size() == 0;
}
});
System.out.println("result is " + isRight);
}
}