Process exec(String command)
描述 (Description)
java.lang.Runtime.exec(String command)方法在单独的进程中执行指定的字符串命令。 这是一种方便的方法。 调用exec(command)形式的行为与调用exec(command,null,null)完全相同。
声明 (Declaration)
以下是java.lang.Runtime.exec()方法的声明
public Process exec(String command)
参数 (Parameters)
command - 指定的系统命令。
返回值 (Return Value)
此方法返回一个用于管理子进程的新Process对象
异常 (Exception)
SecurityException - 如果存在安全管理器且其checkExec方法不允许创建子进程
IOException - 如果发生I/O错误
NullPointerException - 如果command为null
IllegalArgumentException - 如果命令为空
例子 (Example)
以下示例显示了lang.Runtime.exec()方法的用法。
package com.iowiki;
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
Process process = Runtime.getRuntime().exec("notepad.exe");
// print another message
System.out.println("Notepad should now open.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Executing notepad.exe
Notepad should now open.