CmdExecute.java
/**
* @(#) CmdExecute.java Created on 2012-7-19
*
* Copyright (c) 2012 Aspire. All Rights Reserved
*/
package com.aspire.sqmp.mobilemanager.service.adb;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* The class <code>CmdExecute</code>
*
* @author wuyanlong
* @version 1.0
*/
public class CmdExecute {
/**
* Execute a command
*
* @param cmd
* @return
* @throws IOException
*/
public static CmdResponse exec(String cmd) throws IOException {
ReadInputStream msgReadding = null;
ReadInputStream errorMsgReadding = null;
CmdResponse respone = new CmdResponse();
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
// 获取进程的错误流
errorMsgReadding = new ReadInputStream(process.getErrorStream());
errorMsgReadding.start();
// 获取正常流信息
msgReadding = new ReadInputStream(process.getInputStream());
msgReadding.start();
// respone
respone.setRunSuccess(process.waitFor() == 0);
respone.setMsg(msgReadding.getMsg());
respone.setErrorMsg(errorMsgReadding.getMsg());
} catch (Exception e) {
respone.setRunSuccess(false);
respone.setErrorMsg(e.getMessage());
e.printStackTrace();
} finally {
try {
if (errorMsgReadding != null)
errorMsgReadding.toStop();
if (msgReadding != null)
msgReadding.toStop();
process.destroy();
} catch (Exception e) {
}
}
return respone;
}
/**
* Execute a command
*
* @param process
* @param command
* @return
*/
public static CmdResponse exec(Process process, String command) {
ReadInputStream msgReadding = null;
ReadInputStream errorMsgReadding = null;
CmdResponse respone = new CmdResponse();
if (process == null) {
respone.setRunSuccess(false);
return respone;
}
DataOutputStream dos = null;
try {
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(command + "\n");
dos.writeBytes("exit\n");
dos.flush();
// 获取进程的错误流
errorMsgReadding = new ReadInputStream(process.getErrorStream());
errorMsgReadding.start();
// 获取正常流信息
msgReadding = new ReadInputStream(process.getInputStream());
msgReadding.start();
// respone
respone.setRunSuccess(process.waitFor() == 0);
respone.setMsg(msgReadding.getMsg());
respone.setErrorMsg(errorMsgReadding.getMsg());
} catch (Exception e) {
respone.setRunSuccess(false);
respone.setErrorMsg(e.getMessage());
e.printStackTrace();
} finally {
try {
if (dos != null)
dos.close();
if (errorMsgReadding != null)
errorMsgReadding.toStop();
if (msgReadding != null)
msgReadding.toStop();
process.destroy();
} catch (Exception e) {
}
}
return respone;
}
/**
*
* The class <code>ReadErrorInput</code>
*
* @author wuyanlong
* @version 1.0
*/
static class ReadInputStream extends Thread {
/**
* Running
*/
private boolean running = true;
/**
* Buffer of reading string
*/
private StringBuffer readBuffer = new StringBuffer();
/**
* BufferedReader
*/
private BufferedReader readBr;
private boolean isReading = false;
/**
*
* Constructor
*
* @param is
*/
public ReadInputStream(InputStream is) {
if (is != null)
readBr = new BufferedReader(new InputStreamReader(is));
}
/**
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
@Override
public void run() {
try {
while (running) {
readding();
sleep(250);
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (readBr != null)
try {
readBr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
super.run();
}
/**
* Reading
*
* @throws IOException
*/
private void readding() throws IOException {
synchronized (this) {
if (isReading)
return;
isReading = true;
}
if (readBr == null)
return;
while (readBr.ready()) {
readBuffer.append(readBr.readLine());
readBuffer.append("\n");
}
synchronized (this) {
isReading = false;
}
}
/**
* To stop thread.
*/
public void toStop() {
running = false;
}
/**
*
* @return
*/
public String getMsg() {
try {
readding();
} catch (IOException e) {
e.printStackTrace();
}
return readBuffer.toString();
}
}
/**
*
* The class <code>CmdRespone</code>
*
* Return response from execute a command
*
* @author wuyanlong
* @version 1.0
*/
public static class CmdResponse {
/**
* Succes of Running
*/
private boolean isRunSuccess;
/**
* Message of success or faild
*/
private String msg;
/**
* Error message
*/
private String errorMsg;
/**
* Getter of isRunSuccess cmdexecute进程运行成功
*
* @return the isRunSuccess
*/
public boolean isRunSuccess() {
return isRunSuccess;
}
/**
* Setter of isRunSuccess
*
* @param isRunSuccess
* the isRunSuccess to set
*/
public void setRunSuccess(boolean isRunSuccess) {
this.isRunSuccess = isRunSuccess;
}
/**
* Getter of msg
*
* @return the msg
*/
public String getMsg() {
return msg;
}
/**
* Setter of msg
*
* @param msg
* the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
}
/**
* Getter of errorMsg
*
* @return the errorMsg
*/
public String getErrorMsg() {
return errorMsg;
}
/**
* Setter of errorMsg
*
* @param errorMsg
* the errorMsg to set
*/
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}
}
分享到:
相关推荐
Windows中SSH Secure Shell Client 的使用方法doc Windows中SSH Secure Shell Client 的使用方法doc
在Windows平台下安装PHP后,在使用PHP的exec、shell_exec等函数时,经常会遇到报错:unable to fork...,这实际上是因为PHP对cmd.exe没有足够的权限引起的。
在使用上一般都是直接利用windows的外壳API做一些工作,因为外壳操作需要一些比较专业的知识,因此,大部分编程人员特别是使用集成编程环境的程序人员对windows shell的扩展编程不很了解,也缺乏这方面的资料。...
linux下调用Shell实现文件上传ftp服务器
Windows下搭建Shell编译环境(3)
windows shell 编程源码
windows shell 编程pdf,关于windows shell 编程详细的介绍
Windows cmd窗口下调试 使用adb shell 可以使用tab键补全
windows下的shell,便于在windows下学习linux
adb shellfor windows adb shellfor windows
在这本书中,你将学到怎样扩展Windows Shell,客户化它的行为和继承应用到Windows Shell中。你还将发现怎样使用和修改它的属性以达到最好的效能,以及调用那些资料欠缺的Shell API 函数的方法
这本书主要讲述了windows shell编程的内容,里面有很多有用的东西值得下载
Windows下搭建Shell编译环境(1)
Microsoft.WindowsAPICodePack.Shell.dll
Windows下搭建Shell编译环境(4)
关于什么叫Windows shell扩展程序,这里不作介绍,不懂的同学请google之。这里采用的开发环境为Windows XP+sp3, VS 2005 + sp1 (应该支持VS 2005以上的VS版本,VC 6.0估计不支持)。
大量windows shell编程例子 - large windows shell programming examples
windows下的shell环境模拟程序集合,从cygwin下提取出来,包含常用的命令集合:basename/dirname/mkdir/mv/rm/cp/sort/split/find/awk/sed/xargs/tar/grep/gzip/zip/unzip/head/tail/ls/cat/uniq/wc/more/scp/ssh/...
《Windows Shell编程》 chm 格式电子书 Windows Shell编程和指导性丛书
Windows命令行Shell命令详细解析和语法