java.lang.Runtime.exec(String command)%uA0方法执行一个单独的进程中指定的字符串命令。这是一个方便的方法。exec(command)调用形式完全相同于调用exec(command, null, null)。
声明
以下是java.lang.Runtime.exec()方法的声明
public Process exec(String command)
参数
-
command%uA0-- 指定的系统命令。
返回值
该方法返回一个新的Process对象,用于管理子进程
异常
-
SecurityException%uA0-- 如果安全管理器存在,并且其checkExec方法不允许创建子进程
-
IOException%uA0-- 如果发生I/ O错误
-
NullPointerException%uA0-- 如果命令是null
-
IllegalArgumentException%uA0--如果命令是空的
例子
下面的例子显示lang.Runtime.exec()方法的使用。
package com.yiibai 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.