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. Python的Multiprocessing多进程实例

    最近在拜读RBG大神的faster-rcnn源码时发现他用了多进程去分阶段处理神经网络,原因如下: # ------------------------------------------------ ...

  2. 正说PropertyValuesProvider的应用

    Github地址:https://github.com/andyslin/spring-ext 编译.运行环境:JDK 8 + Maven 3 + IDEA + Lombok spring-boot: ...

  3. 【HANA系列】SAP HANA SQL获取字符串长度

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取字符 ...

  4. TCP协议基础(一)

    TCP为应用层提供的服务 提供进程和进程之间的通信 答: 怎么区分服务目的主机上的哪个进程呢? 通过提供端口号(well-known port), 如Telnet 23,DNS 53, HTTP 80 ...

  5. Caused by: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.的几种原因

    环境:centos 7+ 1.查看用户是否存在 进入安装目录使用./sbin/rabbitmqctl list_users查看是否存在用户 比如:./usr/local/rabbitmq/rabbit ...

  6. 解决pip安装第三方包编码错误:UnicodeDecodeError: 'ascii' codec can't decode byte....

    .../python27/Lib/mimetypes.py 在 import之后添加下列内容 if sys.getdefaultencoding() != 'gbk': reload(sys) sys ...

  7. mint ui解决Navbar和Infinite scroll共存时的bug

    Navbar和Infinite scroll共同使用时会出现无限加载的问题,滑动也会出现乱加载. 只需要判断一下就可以了,代码: html: <mt-navbar v-model="s ...

  8. 【Spring 基础】通过注解注入Bean

    原课程:通过注解注入Bean 注入bean知识点思维导图 Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection spring为什么推荐使用构 ...

  9. skiplist(跳表)的原理及JAVA实现

    前记 最近在看Redis,之间就尝试用sortedSet用在实现排行榜的项目,那么sortedSet底层是什么结构呢? "Redis sorted set的内部使用HashMap和跳跃表(S ...

  10. python之函数、面向对象

    学习python到了函数这一块进度有所放缓,主要还是想理解透彻,毕竟直觉告诉我函数是python是其中的关键,不管是模块.还是包.或者是库,都是建立在若干个函数定义上面. 章节后面就是关于面向对象编程 ...