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代码传参数 ...
随机推荐
- Jmeter实现WebSocket协议的接口
1.下载websocket插件的jar包 网盘链接:https://pan.baidu.com/s/1FDcTHdQcDo6izgROMgB96w 密码:uags 该包下载完成后直接放在jmeter的 ...
- PJzhang:关闭wps小广告和快速关闭445端口
猫宁!!! kali linux上安装的wps,没有广告,而且轻巧简洁. 如果你在windows上安装wps,除了ppt.word.excel,还会有一个h5的应用,当然,最令人烦扰的当 ...
- python 实例化 类方法 静态方法 成员变量 实例方法 等调用
1.参考代码如下 # coding:utf-8 class student: # 成员变量 ok = None like = '八戒你瘦了' # 实例方法 def __init__(self): # ...
- 【Qt开发】【计算机视觉】OpenCV在Qt-MinGw下的编译库
最近电脑重装系统了,第一件事重装OpenCV.这次直接装最新版,2014-4-25日发布的OpenCV2.4.9版本,下载链接: http://sourceforge.NET/projects/ope ...
- hadoop的单机配置
hadoop的单机配置 准备工作 利用vim /etc/sysconfig/network命令修改主机名称. Ssh security shell 远程登录 登录远程服务器 $ ssh user@ho ...
- python random 的用法
python random的里面的方法其实是Random实例化的对象. 里面几个常用的几个方import random print( random.randint(1,10) ) # 产生 1 到 1 ...
- Sql Server 常见的几种分页方式
⒈offset fetch next方式[SqlServer2012及以上版本支持][推荐] select * from T_User order by id offset rows /*(页数-1) ...
- Elasticsearch6.2集群搭建, centos7
原文地址,转载请注明出处:https://blog.csdn.net/qq_34021712/article/details/79330028 ©王赛超 环境介绍 服务器 是否可以成为主节点 是否 ...
- Python应用RabbitMQ教程
介绍 RabbitMQ是一个消息代理.它的工作就是接收和转发消息.你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处.在这个比喻中,RabbitMQ就扮演着邮箱.邮局以及邮 ...
- 简单 UDP 操作类
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Ne ...