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. UITableView的使用总结

    直接贴代码了,很好理解,注释很全,一看就懂...... // // ViewController.m // TableViewSectionTitleDemo // // Created by 思 彭 ...

  2. iOS10权限问题

    下图就是Info.plist的常用的权限问题: * 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? * 相机权限: Priv ...

  3. ERROR】Unable to open underlying table which is differently defined or of non-MyISAM type or ...

    Error: Unable to open underlying table which is differently defined or of non-MyISAM type or doesn’t ...

  4. Android开发 互相调用模式之C#主导

    首先明确一个概念,当我们不使用Android Studio提供的那些包,仅仅是Unity打包apk,打包出来的apk里面也包含了SDK (1)首先删除Unity下我们创建的Plugins文件夹,因为这 ...

  5. Sql Service 存储过程、触发器

    if exists (select * from sysobjects where name='tb_admin') drop table tb_admin go create table tb_ad ...

  6. 深入理解java:4.3.1. 框架编程之MyBatis---SQL语句执行的完整流程

    Mybatis的整个的执行流程.如下图所示: 原理详解: MyBatis应用程序根据XML配置文件创建SqlSessionFactory, SqlSessionFactory在根据配置,配置来源于两个 ...

  7. jmeter—获取当前时间(年、月、日),往前/往后n天

    import java.util.Calendar; Calendar cal = Calendar.getInstance(); int day = cal.get(Calendar.DATE); ...

  8. pikachu-SQL注入

    参考网址: http://www.mamicode.com/info-detail-2795438.html

  9. leveldb Arena源码分析

    前言 对于一个高性能的服务器程序来说,内存的使用非常重要.C++提供new/delete来管理内存的申请和释放,但是对于小对象来说,直接使用new/delete代价比较大,要付出额外的空间和时间,性价 ...

  10. [Python3] 040 文件 一般使用

    目录 文件 1. open 函数 2. with 语句 3. 先写再读 3.1 写 3.2 读 4. "位置"的查询与移动 4.1 tell() 4.2 seek(cookie, ...