您的位置:首页 > 编程语言 > Java开发

java 使用java执行命令简易封装类(未测试)

2016-07-22 12:26 495 查看
在java中有时我们会调用系统命令或批处理或shell脚本,前几天项目需要我就简单的写了下.可供大家参考下.使用java执行命令简易封装类.

使用java执行命令简易封装类

package my.utils.exec;

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 , null , 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 , null , null);
}
/**
*
* @描述 在有指定工作目录的独立进程中执行指定的字符串命令。
* @作者 钟诚
* @日期 Sep 17, 2011
* @时间 11:17:59 AM
* @param commands 包含所调用命令及其参数的数组。例如:new String[]{"/home/user1/test.sh","arg1","arg2"};
* @param workpath 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为null。
* @throws IOException
*/
public void exec(String[] commands,String workpath) throws IOException {
exec(commands , null , workpath);
}

/**
*
* @描述 在有指定环境和工作目录的独立进程中执行指定的字符串命令。
* @作者 钟诚
* @日期 Sep 9, 2011
* @时间 5:18:00 PM
* @param commands 包含所调用命令及其参数的数组。例如:new String[]{"/home/user1/test.sh","arg1","arg2"};
* @param envp 环境变量,字符串数组,其中每个元素的环境变量设置格式为 name=value;如果子进程应该继承当前进程的环境,则为null。
* @param path 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为null。
* @throws IOException
*/
public void exec(String[] commands,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】>>>:"+getCommandString(commands))
a52e
;
is = Runtime.getRuntime().exec(commands,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 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);
}

使用java执行命令简易封装类

package my.utils.exec;

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);
}
}

使用java执行命令简易封装类

package my.utils.exec;

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" });
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: