您是否为 java 运行时探索过这个 exec 命令,使用要“cd”到的路径创建一个文件对象,然后将其作为 exec 方法的第三个参数输入。
public Process exec(String command,
String[] envp,
File dir)
throws IOException
在具有指定环境和工作目录的单独进程中执行指定的字符串命令。
这是一种方便的方法。exec(command, envp, dir) 形式的调用与调用 exec(cmdarray, envp, dir) 的行为方式完全相同,其中 cmdarray 是命令中所有标记的数组。
更准确地说,使用由调用 new StringTokenizer(command) 创建的 StringTokenizer 将命令字符串分解为标记,而无需进一步修改字符类别。标记器生成的标记然后以相同的顺序放置在新的字符串数组 cmdarray 中。
Parameters:
command - a specified system command.
envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
Returns:
A new Process object for managing the subprocess
Throws:
SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess
IOException - If an I/O error occurs
NullPointerException - If command is null, or one of the elements of envp is null
IllegalArgumentException - If command is empty