Process exec(String command, String[] envp)
描述 (Description)
java.lang.Runtime.exec(String command, String[] envp)方法在具有指定环境的单独进程中执行指定的字符串命令。 这是一种方便的方法。 调用exec(command,envp)形式的行为与调用exec(command,envp,null)完全相同。
声明 (Declaration)
以下是java.lang.Runtime.exec()方法的声明
public Process exec(String command, String[] envp)
参数 (Parameters)
command - 指定的系统命令。
envp - 字符串数组,其每个元素的格式为name = value的环境变量设置,如果子进程应继承当前进程的环境,则为null。
返回值 (Return Value)
此方法返回一个用于管理子进程的新Process对象
异常 (Exception)
SecurityException - 如果存在安全管理器且其checkExec方法不允许创建子进程
IOException - 如果发生I/O错误
NullPointerException - 如果command为null,或者envp的某个元素为null
IllegalArgumentException - 如果命令为空
例子 (Example)
以下示例显示了lang.Runtime.exec()方法的用法。
package com.iowiki;
import java.io.File;
public class RuntimeDemo {
public static void main(String[] args) {
try {
// print a message
System.out.println("Executing notepad.exe...");
// create a process and execute notepad.exe and currect environment
Process process = Runtime.getRuntime().exec("notepad.exe", null);
// print another message
System.out.println("Notepad should now open.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Executing notepad.exe...
Notepad should now open.