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代码传参数 ...
随机推荐
- style属性
style加样式是加在行间,取样式也是在行间取: 我们来看下面这段代码: <!DOCTYPE HTML> <html> <head> <meta charse ...
- java:zookeeper集群配置,dubbo
1.zookeeper集群配置: 2.dubbo:(配置见视频)
- BCP 数据导出
EXEC master..xp_cmdshell 'BCP test.dbo.name out d:\t_002.txt -c -t -T' EXEC master..xp_cmdshell 'BCP ...
- etcd 使用: golang 例子
一:连接到 etcd package main import ( "fmt" "go.etcd.io/etcd/clientv3" "time&quo ...
- Go语言集成开发工具JetBrains GoLandMac2.3中文版
JetBrAIns GoLand for Mac是是专为Go开发人员构建的跨平台IDE,功能非常强大拥有强大的代码洞察力,帮助所有Go开发人员即时错误检测和修复建议,快速和安全的重构,一步撤销,智能 ...
- web端测试之封装公共部分
from time import * from selenium import webdriver def login(self,username,passwd): self.dr=webdriver ...
- 【机器学习理论】概率论与数理统计--假设检验,卡方检验,t检验,F检验,方差分析
显著性水平α与P值: 1.显著性水平是估计总体参数落在某一区间内,可能犯错误的概率,用α表示. 显著性是对差异的程度而言的,是在进行假设检验前确定的一个可允许作为判断界限的小概率标准. 2.P值是用来 ...
- c++ 读入优化通用模板
struct ioss { #define endl '\n' ; char obuf[LEN], *oh = obuf; std::streambuf *fb; ioss() { ios::sync ...
- win32 API 笔记1
//==================HOOK钩子========================= 可以用来HOOK掉系统对某些函数的的调用 //==================终止系统进程= ...
- [转帖]全方位掌握OpenStack技术知识
全方位掌握OpenStack技术知识 http://www.itpub.net/2019/06/17/2206/ 架构师技术联盟的文章 相当好呢. 大家好,我是小枣君.最近几年,OpenStack这个 ...