JAVA实现实时Process,实时获取输出
2015-07-13 14:27
393 查看
如题,java实现实时Process,我这里采用ProcessBuilder类获取一个Process实例
具体看代码:RealtimeProcess.java
再来个使用RealtimeProcess的demo:Main.java
执行的命令的内容:C:\Documents and Settings\Administrator\桌面\a.bat
源码附件
具体看代码:RealtimeProcess.java
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class RealtimeProcess{ // 是否在执行 private boolean isRunning = false; // 存放命令行 private ArrayList<RealtimeProcessCommand> commandList = new ArrayList<RealtimeProcessCommand>(); // 保存所有的输出信息 private StringBuffer mStringBuffer = new StringBuffer(); private ProcessBuilder mProcessBuilder = null; private BufferedReader readStdout = null; private BufferedReader readStderr = null; // 回调用到的接口 private RealtimeProcessInterface mInterface = null; private int resultCode = 0; private String ROOT_DIR = null; private String tmp1 = null; private String tmp2 = null; public RealtimeProcess(RealtimeProcessInterface mInterface){ // 实例化接口对象 this.mInterface = mInterface; } public void setCommand(String ...commands){ // 遍历命令 for(String cmd : commands){ RealtimeProcessCommand mRealtimeProcessCommand = new RealtimeProcessCommand(); if(ROOT_DIR != null) mRealtimeProcessCommand.setDirectory(ROOT_DIR); mRealtimeProcessCommand.setCommand(cmd); commandList.add(mRealtimeProcessCommand); } } public void setDirectory(String directory){ this.ROOT_DIR = directory; } public void start() throws IOException, InterruptedException{ isRunning = true; for(RealtimeProcessCommand mRealtimeProcessCommand : commandList){ mProcessBuilder = new ProcessBuilder(); if(ROOT_DIR != null) mProcessBuilder.directory(new File(mRealtimeProcessCommand.getDirectory())); // 不重定向错误输出 mProcessBuilder.redirectErrorStream(false); mProcessBuilder.command(mRealtimeProcessCommand.getCommand()); exec(mProcessBuilder.start()); } } public String getAllResult(){ return mStringBuffer.toString(); } private void exec(final Process process) throws InterruptedException{ // 获取标准输出 readStdout = new BufferedReader(new InputStreamReader(process.getInputStream())); // 获取错误输出 readStderr = new BufferedReader(new InputStreamReader(process.getErrorStream())); // 创建线程执行 Thread execThread = new Thread(){ public void run(){ try { // 逐行读取 while((tmp1 = readStdout.readLine()) != null || (tmp2 = readStderr.readLine()) != null){ if(tmp1 != null){ mStringBuffer.append(tmp1 + "\n"); // 回调接口方法 mInterface.onNewStdoutListener(tmp1); } if(tmp2 != null){ mStringBuffer.append(tmp2 + "\n"); mInterface.onNewStderrListener(tmp2); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } resultCode = process.exitValue(); } }; execThread.start(); execThread.join(); isRunning = false; mInterface.onProcessFinish(resultCode); } public boolean isRunning(){ return this.isRunning; } public int getCommandSize(){ return commandList.size(); } public RealtimeProcessCommand getRealtimeProcessCommand(int p){ return commandList.get(p); } } class RealtimeProcessCommand{ private String directory = null; private String command = null; public RealtimeProcessCommand(){} public void setDirectory(String directory){ this.directory = directory; } public void setCommand(String command){ this.command = command; } public String getDirectory(){ return this.directory; } public String getCommand(){ return this.command; } } interface RealtimeProcessInterface{ void onNewStdoutListener(String newStdout); void onNewStderrListener(String newStderr); void onProcessFinish(int resultCode); //void execCommand(String ...commands); }
再来个使用RealtimeProcess的demo:Main.java
import java.io.IOException; public class Main implements RealtimeProcessInterface{ /** * @param args */ private RealtimeProcess mRealtimeProcess = null; public static void main(String[] args) { // TODO Auto-generated method stub new Main().test(); } public void test(){ mRealtimeProcess = new RealtimeProcess(this); //mRealtimeProcess.setDirectory(""); mRealtimeProcess.setCommand("C:\\Documents and Settings\\Administrator\\桌面\\a.bat"); try { try { mRealtimeProcess.start(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(mRealtimeProcess.getAllResult()); } public void onNewStdoutListener(String newStdout) { // TODO Auto-generated method stub System.out.println("==>STDOUT > " + newStdout); } public void onNewStderrListener(String newStderr) { // TODO Auto-generated method stub System.out.println("==>STDERR > " + newStderr); } public void onProcessFinish(int resultCode) { // TODO Auto-generated method stub System.out.println("==>RESULT_CODE > " + resultCode); } }
执行的命令的内容:C:\Documents and Settings\Administrator\桌面\a.bat
ping www.baidu.com exit 888
源码附件
相关文章推荐
- 关于MyEclipse6.5 总是弹出 Update Progress(xx-xx-xx时间)的问题
- 使用Spring Boot快速构建应用
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)
- 深入Struts2的配置文件
- Java多线程
- Javah导出类问题
- JDK版本不一致导致导入的项目报错
- java.net URL类的使用
- java集合
- java PKCS7Padding 加密Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法
- springMVC下的json数据的传递
- java.net.URL请求远程文件下载
- Java EE实现模拟一个资源管理器
- Spring官网改版后下载
- eclipse的maven项目中target目录m2e-wtp无法提交到SVN的处理办法
- java面向对象简单理解
- 【Java 它 JVM】对象的创建过程
- Spring学习笔记——Sprin管理Bean的原理
- Eclipse maven构建springmvc项目
- Java的JXL操作xls形式