public class ShellCommand
{
    public static void execCmd(String cmd, boolean wait)
    {
        execCmd(cmd, wait, null);
    }

public static void execCmd(String cmd, boolean wait, StringBuilder output)
    {
        String[] cmds = new String[] { cmd };
        execCmds(cmds, null, null, wait, output);
    }

public static void execCmds(String[] cmd, String dir, boolean wait,
            StringBuilder output)
    {
        execCmds(cmd, null, dir, wait, output);
    }

public static void execCmds(String[] cmds, String[] env, String dir,
            boolean wait, StringBuilder output)
    {
        Process process = null;
        try {
            File dirFile = null;
            if (dir != null) {
                dirFile = new File(dir);
            }
            for (String cmd : cmds) {
                System.out.println(cmd);
            }
            if (cmds.length == 1) {
                process = Runtime.getRuntime().exec(cmds[0], env, dirFile);
            } else {
                process = Runtime.getRuntime().exec(cmds, env, dirFile);
            }
//            logProcessOutput(process, cmds[0], output);

} catch (Exception e) {
            String message = "executeCmd: " + cmds + " error: " + e.toString();
            if (output != null) {
                output.append(message);
            }
        }
        if (process != null) {
            if (wait) {
                try {
                    process.waitFor();
                    process.getOutputStream().close();
                    process.getErrorStream().close();
                    process.getInputStream().close();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

public static Process runProcess(String[] cmds, boolean root, String[] env,
            String dir, boolean wait, StringBuilder output)
    {
        Process process = null;
        try {
            File dirFile = null;
            if (dir != null) {
                dirFile = new File(dir);
            }
            for (String cmd : cmds) {
                System.out.println(cmd);
            }
            if (root) {
                process = Runtime.getRuntime().exec("su");
                DataOutputStream os = new DataOutputStream(
                        process.getOutputStream());
                for (String cmd : cmds) {
                    os.writeBytes(cmd + "\n");
                }
                os.flush();
                // os.writeBytes("exit\n");
                os.close();
            } else {
                if (cmds.length == 1) {
                    process = Runtime.getRuntime().exec(cmds[0], env, dirFile);
                } else {
                    process = Runtime.getRuntime().exec(cmds, env, dirFile);
                }
            }
        } catch (Exception e) {
            String message = "executeCmd: " + cmds + " error: " + e.toString();
            if (output != null) {
                output.append(message);
            }
        }
        if (process != null) {
            if (wait) {
                try {
                    process.waitFor();
                    process.getOutputStream().close();
                    process.getErrorStream().close();
                    process.getInputStream().close();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return process;
    }

}

java代码调用exe(cmd命令)的更多相关文章

  1. Java代码调用服务器上的Shell脚本

    Java代码调用服务器上的Shell脚本 这里主要是因为我们报表平台有用到用户手工录入的数据作为结果数据且需要纳入saiku去展示 如我们所知,saiku不会自动刷新,所以需要在数据更新接口中调用服务 ...

  2. SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法

    create proc insertLog@Title nvarchar(50),@Contents nvarchar(max),@UserId int,@CreateTime datetimeasi ...

  3. Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件

    本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...

  4. Java代码调用Oracle的存储过程,存储函数和包

    Java代码调用存储过程和存储函数要使用CallableStatement接口 查看API文档: 上代码: java代码调用如下的存储过程和函数: 查询某个员工的姓名  月薪 职位 create or ...

  5. java代码调用数据库存储过程

    由于前边有写java代码调用数据库,感觉应该把java调用存储过程也写一下,所以笔者补充该篇! package testSpring; import java.sql.CallableStatemen ...

  6. java调用执行cmd命令

    未经允许,禁止转载!!! package practice; import java.io.BufferedReader; import java.io.File; import java.io.IO ...

  7. java远程调用linux的命令或者脚本

    转载自:http://eksliang.iteye.com/blog/2105862 Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar) 使用步骤如下 ...

  8. java执行windows 的cmd 命令

    //获取运行时 Runtime rt = Runtime.getRuntime(); //获取进程 Process p = rt.exec(String[] cmdarray);     或者   P ...

  9. 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql

    1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件  特地将执行map的个数设置为变量  测试 可以java代码传参数 ...

随机推荐

  1. iOS去除数组中重复的model数据

    // 去除数组中model重复 ; i < self.selectedModelArray.count; i++) { ;j < self.selectedModelArray.count ...

  2. Go package(2) strings 用法

    go version go1.10.3 Go中的字符串用法,可以在 godoc.org 上查看语法和用法. 最简单的语法就是获取字符串中的子串 s := "hello world" ...

  3. python-阿里镜像源-pip

    将pip源设置为国内 >>>windows 1 地址栏输入%appdata% 2 新建pip文件价 3 文件夹里建pip.ini 内容: [global] timeout = 600 ...

  4. Mybatis是什么?mybatis中的对一和对多关系怎么配置

    Mybatis是什么? 1.mybatis出来之前,由java的jdbc连接数据库,mybatis出来之后,将jdbc进行封装,实现更有效的连接:   2.mybatis的对象SqlSession,s ...

  5. 配置传统vlan间路由

    S1#SH RUN spanning-tree mode pvst ! interface FastEthernet0/1 switchport access vlan 10 switchport m ...

  6. 关于deepin装机的一些经验总结

    最近win10用的有一点腻,虚拟机很久之前就装上了deepin,乌班图,红帽等隶属于Linux一些操作系统,但是终究还是虚拟机所以还是想在真机上试试,所以照着deepin网上的流程装好了deepin并 ...

  7. 使用PowerShell 在域内远程安装DFS

    # 安装DFS 命名空间.DFS 管理工具.DFS 复制# author:lttr <www.cnblogs.com/GoCircle> # date:2019-08-09 # eg. # ...

  8. 【转帖】SQL Server 各版本发布时间和开发代号

    SQL Server 各版本发布时间和开发代号 2019年01月23日 11:07:44 努力挣钱娶媳妇的苗同学 阅读数 278 https://blog.csdn.net/weixin_446098 ...

  9. IE浏览器(js)new Date()带参返回NaN解决方法

    function myNewDate(str) { if(!str){ return 0; } arr=str.split(" "); d=arr[0].split("- ...

  10. C++;STL--队列与栈;

    队列 queue模板类的定义在<queue>头文件中. queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型. 定 ...