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. Eclipse进行Debug时断点上有一个斜杠,并且debug没有停在断点处

    断点上有斜杠,这是由于设置了Skip All Breakpoints的缘故,调试会忽略所有断点,执行完,只需取消Skip All Breakpoints即可,操作:Run-->Skip All ...

  2. [LeetCode] Top 100 Liked Questions

    [LeetCode] Top 100 Liked Questions # Title Acceptance Difficulty 1 Two Sum 38.80% Easy 2 Add Two Num ...

  3. 爱伪装(AWZ)/爱立思(ALS)改机改串一键新机原理分析

    简介 爱伪装(AWZ)/爱立思(ALS)是一款iOS越狱系统上的改机工具,可以修改多种系统参数达到伪装设备型号及各种软硬件属性的目的,同时提供了防越狱检测机制,常用于iOS上的推广刷量,配合代理/VP ...

  4. 利用Fiddler-ImageView识别图像信息及优化图像

    一般情况下,我们用Fiddler来拦截修改数据包,分析数据包,但很少拿它来分析图片. Fiddler里的ImageView视图不仅仅能显示图片,还能解析图片里包含的信息,比如帧数,图片修改时间,版权信 ...

  5. Excel随机数相关

    基本函数 RAND() 函数:自动生成一个[0,1)的平均分布随机数(依重新计算而改变) RANDBETWEEN(bottom,top) :返回一个介于指定数字直接的随机数,不会自动改变 INT(nu ...

  6. 【AMAD】Pysnooper -- 别再用print进行debug了

    简介 动机 作用 用法 热度分析 源码分析 个人评分 简介 pysnooper是低配版debugger,别再用print进行debug了. 动机 想象一个场景,你的Python代码运行后不产生你要的效 ...

  7. Java内存模型(二)volatile底层实现(CPU的缓存一致性协议MESI)

    CPU的缓存一致性协议MESI 在多核CPU中,内存中的数据会在多个核心中存在数据副本,某一个核心发生修改操作,就产生了数据不一致的问题,而一致性协议正是用于保证多个CPU cache之间缓存共享数据 ...

  8. c语言:<tchar.h>

    头文件“<tchar.h>”作用就是为了进行ASCII码和UNICODE(wide-character)码的头文件(该头文件由微软提供): 这样我们就可以使用TCHAR.H头文件中的定义的 ...

  9. yield浅析-Python3

    yield 浅析 先来一段代码: def fun1(): for i in range(5): yield i print("继续调用继续执行") gen1 = fun1() pr ...

  10. js实现复制内容到剪贴板

    一. 原生js实现,电脑可以用,手机不可以用 1. 必须是 input元素 才可以使用 <input id="code" type="text" valu ...