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代码传参数 ...
随机推荐
- localStorage基本了解及使用
以下内容来自: https://www.cnblogs.com/st-leslie/p/5617130.html 感谢大佬的分享 一.什么是localStorage.sessionStorage 在 ...
- docker 的安装和镜像
一.docker的 安装 : 第一种: yum -y install docker systemctl start docker.service systemctl status docker 第二种 ...
- 运行python manage.py 出现mportError: No module named django.core.management when using manage.py
1 . linux下用virtualenv 创建虚拟空间环境没有安装djang,即使主机装了,否则运行python manage.py 出现mportError: No module named dj ...
- python-爬虫-史书典籍
import requests import os from lxml import html import time def get_title_url(tree): '''一级 获取标题''' # ...
- 【ABAP系列】SAP 使用特殊的技术更新数据库(ABAP)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 使用特殊的技术更新数据库 ...
- Django学习笔记(一)Django基础
新建项目 django-admin startproject my_site #会在当前目录新建my_site目录,可自行修改目录名 django-admin startproject my_site ...
- appium环境搭建(一)----安装appium
一.安装appium Appium官方网站:http://appium.io/,官方首页给出了appium的安装步骤. > brew install node # get node.js > ...
- 【转贴】Debian 10 "buster" 正式发布
Debian 10 "buster" 正式发布 https://news.cnblogs.com/n/627909/ 我看到龙芯的 就是 mips64el 的指令集.. Linux ...
- [转帖]linux下使用 du查看某个文件或目录占用磁盘空间的大小
linux下使用 du查看某个文件或目录占用磁盘空间的大小 du -ah --max-depth= 去年用过一次 后来忘记了.. 命令这个东西 熟能生巧.. https://www.cnblogs.c ...
- 使用HTMLTestRunner生产报告
HTMLTestRunner下载安装及用法 1. 说明 HTMLTestRunner 是 Python 标准库的 unittest 模块的一个扩展.它生成易于使用的 HTML 测试报告 本文针对Pyt ...