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. 重置csr

    重置csr 注意:下面操作仅在刚安装k8s后24小时内有效 分析:kubelet启动后会生成如下文件.kubelet.conf文件决定了csr的存在,如果要想重新获取csr,可以停掉kubelet,删 ...

  2. mysql 索引失效的几种情况+

  3. 2单表CRUD综合样例开发教程

    东软集团股份有限公司 基础软件事业部 单表CRUD综合样例开发教程 东软机密 tui 更改履历 版本号 更改时间 更改的 图表和章节号 状态 更改简要描述 更改申 请编号 更改人 批准人 V1.0 2 ...

  4. django中使用mysql数据库

    django连接mysql 安装MySQL 1 linux: apt install mysql-server apt install mysql-client mysql_secure_instal ...

  5. 把CSV文件中的labels标签提取为json文件

    需求: validationImages.csv文件是存储验证集数据名称和类别信息(labels)的文件, 要生成一个label和类别名一一对应且正序排列的json文件,代码如下: labels_di ...

  6. Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices)

    Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices) 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为 ...

  7. Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings)

    Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings) 给定两个字符串s1, s2,找到 ...

  8. Identification of Encryption Algorithm Using Decision Tree

    本文主要做了两件事,一是提出了一种使用C4.5算法生成的决策树来识别密文所使用的加密算法的方法,二是为这一算法设计了一个特征提取系统提取八个特征作为算法的输入,最终实现了70%~75的准确率. 准备工 ...

  9. java将base64解析图片保存到本地。

    将base64解析图片保存到本地的两个方法 /** * base64转图片 * @param base64str base64码 * @param savePath 图片路径 * @return */ ...

  10. MSF魔鬼训练营-3.2.1活跃主机扫描

    概要: msf的arp_sweep .udp_sweep模块 Nmap -sn使用ping探测 -PU -sn 使用UDP协议端口探测 msf模块 arp_sweep     常用 ipv6_mult ...