使用java执行命令简易封装类
发布时间:2020-05-24 19:27:29 所属栏目:Java 来源:互联网
导读:使用java执行命令简易封装类
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 在java中有时我们会调用系统命令或批处理或shell脚本import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public abstract class ExecCommand {
private static final Log log = LogFactory.getLog(ExecCommand.class);
/**
*
* @描述 在单独的进程中执行指定的字符串命令。
* @作者 钟诚
* @日期 Sep 9,2011
* @时间 5:15:48 PM
* @param command 一条指定的系统命令
* @throws IOException
*/
public void exec(String command) throws IOException {
exec(command,null,null);
}
/**
*
* @描述 在有指定工作目录的独立进程中执行指定的字符串命令。
* @作者 钟诚
* @日期 Sep 17,2011
* @时间 11:17:59 AM
* @param command 一条指定的系统命令
* @param workpath 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为null。
* @throws IOException
*/
public void exec(String command,String workpath) throws IOException {
exec(command,workpath);
}
/**
*
* @描述 在有指定环境和工作目录的独立进程中执行指定的字符串命令。
* @作者 钟诚
* @日期 Sep 17,2011
* @时间 11:21:28 AM
* @param command 一条指定的系统命令
* @param envp 环境变量,字符串数组,其中每个元素的环境变量设置格式为 name=value;如果子进程应该继承当前进程的环境,则为null。
* @param path 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为null。
* @throws IOException
*/
public void exec(String command,String[] envp,String workpath) throws IOException {
InputStream is = null;
BufferedInputStream in = null;
BufferedReader br = null;
try {
File dir=null;
if(null != workpath)
dir=new File(workpath);
log.info("【COMMAND】>>> "+command);
// InputStream is = Runtime.getRuntime().exec(new String[]{"ping","127.0.0.1"}).getInputStream();
is = Runtime.getRuntime().exec(command,envp,dir).getInputStream();
in = new BufferedInputStream(is);
br = new BufferedReader(new InputStreamReader(in));
String ss = "";
while ((ss = br.readLine()) != null) {
lineHandler(ss);
}
} finally {
if (null != br)
br.close();
if (null != in)
in.close();
if (null != is)
is.close();
}
}
/**
*
* @描述 在单独的进程中执行指定的命令和参数。
* @作者 钟诚
* @日期 Sep 9,2011
* @时间 5:15:48 PM
* @param commands 包含所调用命令及其参数的数组。例如:new String[]{"/home/user1/test.sh","arg1","arg2"};
* @throws IOException
*/
public void exec(String[] commands) throws IOException {
exec(commands,2011
* @时间 11:17:59 AM
* @param commands 包含所调用命令及其参数的数组。例如:new String[]{"/home/user1/test.sh","arg2"};
* @param workpath 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为null。
* @throws IOException
*/
public void exec(String[] commands,String workpath) throws IOException {
exec(commands,workpath);
}
/**
*
* @描述 在有指定环境和工作目录的独立进程中执行指定的字符串命令。
* @作者 钟诚
* @日期 Sep 9,2011
* @时间 5:18:00 PM
* @param commands 包含所调用命令及其参数的数组。例如:new String[]{"/home/user1/test.sh","arg2"};
* @param envp 环境变量,字符串数组,其中每个元素的环境变量设置格式为 name=value;如果子进程应该继承当前进程的环境,则为null。
* @param path 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为null。
* @throws IOException
*/
public void exec(String[] commands,String workpath) throws IOException {
InputStream is = null;
BufferedInputStream in = null;
BufferedReader br = null;
try {
File dir=null;
if(null != workpath)
dir=new File(workpath);
log.info("【COMMAND】>>>:"+getCommandString(commands));
is = Runtime.getRuntime().exec(commands,dir).getInputStream();
in = new BufferedInputStream(is);
br = new BufferedReader(new InputStreamReader(in));
String ss = "";
while ((ss = br.readLine()) != null) {
lineHandler(ss);
}
} finally {
if (null != br)
br.close();
if (null != in)
in.close();
if (null != is)
is.close();
}
}
/**
*
* @描述 仅为日志输出,无其他作用
* @作者 钟诚
* @日期 Sep 13,2011
* @时间 1:48:06 PM
* @param commands
* @return
*/
private String getCommandString(String[] commands){
StringBuffer sb=new StringBuffer();
for(String command:commands){
sb.append(command);
sb.append(" ");
}
return sb.toString();
}
/**
*
* @描述 行处理
* @作者 钟诚
* @日期 Sep 9,2011
* @时间 5:22:11 PM
* @param lineStr
*/
protected abstract void lineHandler(String lineStr);
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ExecuteCommand extends ExecCommand {
private static final Log log = LogFactory.getLog(ExecuteCommand.class);
/**
* @描述 执行命令返回行内容处理默认直接输出到日志<br />
* 如果需要自定义处理请覆盖此方法。
* @作者 钟诚
* @时间 2011-09-09 17:15
*/
@Override
protected void lineHandler(String lineStr) {
log.info(lineStr);
}
}
public class ExecCommandTest {
public static void main(String[] args) throws Exception {
// 默认处理
ExecCommand exe1 = new ExecuteCommand();
exe1.exec("c:ls.bat");
// 自定义处理
ExecCommand exe2 = new ExecuteCommand() {
@Override
protected void lineHandler(String lineStr) {
System.out.println(lineStr);
}
};
exe2.exec("c:ls.bat","c:");
// 多个参数
ExecCommand exe3 = new ExecuteCommand();
exe3.exec(new String[] { "ping","127.0.0.1" });
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
