java 执行shell命令,并获取执行结果
2017-12-13 14:47
696 查看
import
com.google.common.base.Strings;
import
org.apache.log4j.Logger;
import
java.io.*;
import
java.util.ArrayList;
import
java.util.List;
/**
* 执行系统命令, 返回执行结果
*
@param
cmd
需要执行的命令
*
@param
dir
执行命令的子进程的工作目录, null 表示和当前主进程工作目录相同
*/
public static
String execCmd(String cmd, File dir) {
StringBuilder result =
new
StringBuilder();
Process process =
null;
BufferedReader bufrIn =
null;
BufferedReader bufrError =
null;
try
{
String[] commond = {"/bin/sh","-c",cmd};
//
执行命令, 返回一个子进程对象(命令在子进程中执行)
process
= Runtime.getRuntime().exec(commond,
null, dir);
//
方法阻塞, 等待命令执行完成(成功会返回0)
process.waitFor();
//
获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
bufrIn
= new
BufferedReader(new
InputStreamReader(process.getInputStream(),
"UTF-8"));
bufrError =
new
BufferedReader(new
InputStreamReader(process.getErrorStream(),
"UTF-8"));
//
读取输出
String
line = null;
while
((line = bufrIn.readLine()) !=
null) {
result.append(line).append('\n');
}
while
((line = bufrError.readLine()) !=
null) {
result.append(line).append('\n');
}
}catch
(Exception e){
logger.error(e);
}finally
{
closeStream(bufrIn);
closeStream(bufrError);
//
销毁子进程
if
(process !=
null) {
process.destroy();
}
}
//
返回执行结果
return
result.toString();
}
private static void
closeStream(Closeable stream) {
if
(stream !=
null) {
try
{
stream.close();
}
catch
(Exception e) {
//
nothing
}
}
}
com.google.common.base.Strings;
import
org.apache.log4j.Logger;
import
java.io.*;
import
java.util.ArrayList;
import
java.util.List;
/**
* 执行系统命令, 返回执行结果
*
@param
cmd
需要执行的命令
*
@param
dir
执行命令的子进程的工作目录, null 表示和当前主进程工作目录相同
*/
public static
String execCmd(String cmd, File dir) {
StringBuilder result =
new
StringBuilder();
Process process =
null;
BufferedReader bufrIn =
null;
BufferedReader bufrError =
null;
try
{
String[] commond = {"/bin/sh","-c",cmd};
//
执行命令, 返回一个子进程对象(命令在子进程中执行)
process
= Runtime.getRuntime().exec(commond,
null, dir);
//
方法阻塞, 等待命令执行完成(成功会返回0)
process.waitFor();
//
获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
bufrIn
= new
BufferedReader(new
InputStreamReader(process.getInputStream(),
"UTF-8"));
bufrError =
new
BufferedReader(new
InputStreamReader(process.getErrorStream(),
"UTF-8"));
//
读取输出
String
line = null;
while
((line = bufrIn.readLine()) !=
null) {
result.append(line).append('\n');
}
while
((line = bufrError.readLine()) !=
null) {
result.append(line).append('\n');
}
}catch
(Exception e){
logger.error(e);
}finally
{
closeStream(bufrIn);
closeStream(bufrError);
//
销毁子进程
if
(process !=
null) {
process.destroy();
}
}
//
返回执行结果
return
result.toString();
}
private static void
closeStream(Closeable stream) {
if
(stream !=
null) {
try
{
stream.close();
}
catch
(Exception e) {
//
nothing
}
}
}
相关文章推荐
- 使用Java代码执行系统命令/shell命令, 并获取输出结果
- java调用shell命令并获取执行结果
- java调用shell命令并获取执行结果
- java调用shell命令并获取执行结果
- java调用shell命令并获取执行结果的示例
- java调用shell命令并获取执行结果
- Java开发之调用shell命令并获取执行结果(Mac)
- java调用shell命令并获取执行结果
- java调用shell命令并获取执行结果
- 服务端用system()执行shell命令获取执行结果(排除signal影响)
- Linux C 执行shell命令并获取返回结果
- Perl 获取shell命令的执行结果
- 获取python执行shell命令的结果
- Java执行linux命令 获取执行结果
- java执行shell命令 获取ip配置信息
- C程序中如何获取shell命令执行结果和返回值
- Java执行命令获取结果并超时退出
- Linux C 程序执行 shell 命令并获取返回结果的方法
- Perl 获取shell命令的执行结果
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值(如果有的话)