Runtime.getRuntime().exec方法
来源:互联网 发布:微团购软件哪个好 编辑:程序博客网 时间:2024/04/29 22:54
Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令。
Runtime.getRuntime().exec共有六个重载方法:
public Process exec(String command)
在单独的进程中执行指定的字符串命令。
public Process exec(String [] cmdArray)
在单独的进程中执行指定命令和变量
public Process exec(String command, String [] envp)
在指定环境的独立进程中执行指定命令和变量
public Process exec(String [] cmdArray, String [] envp)
在指定环境的独立进程中执行指定的命令和变量
public Process exec(String command,String[] envp,File dir)
在有指定环境和工作目录的独立进程中执行指定的字符串命令
public Process exec(String[] cmdarray,String[] envp,File dir)
在指定环境和工作目录的独立进程中执行指定的命令和变量
我们先来比较exec(String command)与exec(String[] cmdArray)的区别,其实他们是等价的,最终都会调用:
exec(String[] cmdarray,String[] envp,File dir),我们看看方法exec(String cmdarray,String[] envp,File dir) throws IOException的实现代码:
public
Process exec(String command, String[] envp, File dir)
throws
IOException {
if
(command.length() ==
0
)
throw
new
IllegalArgumentException(
"Empty command"
);
StringTokenizer st =
new
StringTokenizer(command);
String[] cmdarray =
new
String[st.countTokens()];
for
(
int
i =
0
; st.hasMoreTokens(); i++)
cmdarray[i] = st.nextToken();
return
exec(cmdarray, envp, dir);
}
从上面的代码,我们可以看出最终调用的代码都是:exec(String[] cmdArray,String envp,File dir)。exec(String command)相当于exec(command,null,null),exec(String[] cmdArray)相当于exec(cmdArray,null,null)。
参数说明:
cmdarray
- 包含所调用命令及其参数的数组。
envp
- 字符串数组,其中每个元素的环境变量的设置格式为name=value,如果子进程应该继承当前进程的环境,或该参数为 null。
dir
- 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为 null。
另外,执行exec(String command)不等同于直接执行command line命令,比如命令:
javap -l xxx > output.txt
这时要用exec(String[] cmdArray)。如例:
Process p = Runtime.getRuntime().exec(
new
String[]{
"/bin/sh"
,
"-c"
,
"javap -l xxx > output.txt"
});
关于返回结果类型:Process,它有几个方法:
1.destroy():杀掉子进程
2.exitValue():返回子进程的出口值,值 0
表示正常终止
3.getErrorStream():获取子进程的错误流
4.getInputStream():获取子进程的输入流
5.getOutputStream():获取子进程的输出流
6.waitFor():导致当前线程等待,如有必要,一直要等到由该 Process
对象表示的进程已经终止。如果已终止该子进程,此方法立即返回。如果没有终止该子进程,调用的线程将被阻塞,直到退出子进程,根据惯例,0
表示正常终止
- Runtime.getRuntime().exec方法
- Runtime.getRuntime().exec方法
- Runtime.getRuntime().exec()方法的使用
- Runtime.getRuntime(). exec( )
- java Runtime.getRuntime().exec
- Runtime.getRuntime().exec
- Runtime.getRuntime().exec()
- Runtime.getRuntime().exec()
- java Runtime.getRuntime().exec()
- Runtime.getRuntime.exec 注意事项
- Runtime.getRuntime().exec()
- Runtime.getRuntime().exec
- Runtime.getRuntime().exec问题
- Runtime.getRuntime().exec(...)使用方法
- 通信-Runtime.getRuntime().exec
- Runtime.getRuntime().exec(strMakePathPath);
- Runtime.getRuntime().exec
- Runtime.getRuntime().exec();
- HTML-3
- HTML-4
- 【SSH网上商城】Incorrect datetime value: '' for column 'pdate' at row 1
- CSS学习01
- JVM飙高排查脚本-结构分析
- Runtime.getRuntime().exec方法
- 吉哥系列故事——临时工计划
- 基于飞凌2440开发板rt3070工作在AP模式 ( 二)
- JAVA AOP 动态代理 的例子
- Object-C--->Swift之(六)函数入门级讲解
- hdu 1403
- 向每一个错误致敬——Android HotFix
- 指针定义字符串和数组定义字符串的区别
- 小明系列故事——买年货(三重背包)