加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:38:08 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 在java中有时我们会调用系统命令或批处理或shell脚本 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;impor

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读