java代码调用exe(cmd命令)
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命令)的更多相关文章
- Java代码调用服务器上的Shell脚本
Java代码调用服务器上的Shell脚本 这里主要是因为我们报表平台有用到用户手工录入的数据作为结果数据且需要纳入saiku去展示 如我们所知,saiku不会自动刷新,所以需要在数据更新接口中调用服务 ...
- SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法
create proc insertLog@Title nvarchar(50),@Contents nvarchar(max),@UserId int,@CreateTime datetimeasi ...
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...
- Java代码调用Oracle的存储过程,存储函数和包
Java代码调用存储过程和存储函数要使用CallableStatement接口 查看API文档: 上代码: java代码调用如下的存储过程和函数: 查询某个员工的姓名 月薪 职位 create or ...
- java代码调用数据库存储过程
由于前边有写java代码调用数据库,感觉应该把java调用存储过程也写一下,所以笔者补充该篇! package testSpring; import java.sql.CallableStatemen ...
- java调用执行cmd命令
未经允许,禁止转载!!! package practice; import java.io.BufferedReader; import java.io.File; import java.io.IO ...
- java远程调用linux的命令或者脚本
转载自:http://eksliang.iteye.com/blog/2105862 Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar) 使用步骤如下 ...
- java执行windows 的cmd 命令
//获取运行时 Runtime rt = Runtime.getRuntime(); //获取进程 Process p = rt.exec(String[] cmdarray); 或者 P ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
随机推荐
- debian系统中添加sudo权限
刚安装好的Debian默认还没有sudo功能.1.安装sudo# apt-get install sudo2.修改 /etc/sudoers 文件属性为可写# chmod +w /etc/sudoer ...
- C#编程 委托 Lambda表达式和事件
委托 如果我们要把方法当做参数来传递的话,就要用到委托.简单来说委托是一个类型,这个类型可以赋值一个方法的引用. 声明委托 在C#中使用一个类分两个阶段,首选定义这个类,告诉编译器这个类由什么字段和方 ...
- C#编程 线程,任务和同步(1) 基础认识
线程 对于所有需要等待的操作,例如移动文件,数据库和网络访问都需要一定的时间,此时就可以启动一个新的线程,同时完成其他任务.一个进程的多个线程可以同时运行在不同的CPU上或多核CPU的不同内核上. 线 ...
- numpy的concatenate实现矩阵拼接
concatenate() 我们先来介绍最全能的concatenate()函数,后面的几个函数其实都可以用concatenate()函数来进行等价操作. concatenate()函数根据指定的维度, ...
- C++中map和unordered_map的用法
1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...
- group_by和having的用法
#encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_,Enum ...
- BUUOJ reverse 不一样的flag
不一样的flag 是不是做习惯了常规的逆向题目?试试这道题,看你在能不能在程序中找到真正的flag!注意:flag并非是flag{XXX}形式,就是一个’字符串‘,考验眼力的时候到了! 注意:得到的 ...
- Oracle密码过期(the password has expired)
1.进入sqlplus模式--sqlplus / as sysdba; 2.查看用户密码的有效期设置(一般默认的配置文件是DEFAULT) SELECT * FROM dba_profiles WHE ...
- 手把手教你用 Strace 诊断问题
早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人,十有八九会建议你用 strace 挂上去看看,不过当你挂上去了,看着满屏翻滚的 ...
- [转帖]通俗易懂的Docker 入门教程
看完此文,妈妈还会担心你docker入不了门? http://www.17coding.info/article/24 上周对象突然心血来潮说想养个小宠物,我问想养啥她又说随便,你看着办!!!这我 ...