Java-进程-Process类-waitFor方法
Java-进程-Process类-waitFor方法
·
1 需求
实验对象分类(cmd命令、start命令):
- 可以在cmd窗口运行的命令
- PING.exe:命令需要执行一段时间
- dir.exe:命令瞬间运行完毕
- notepad.exe:无法在cmd窗口运行的命令
结论:
- process.waitFor()中process是指cmd.exe,而不是notepad.exe
结论:如果希望process.waitFor()生效,
- cmd /c notepad:满足要求
- cmd /k notepad:不满足要求
- cmd /c start notepad:不满足要求
- cmd /k start notepad:不满足要求
2 接口
3.1 cmd /c notepad
import java.io.IOException;
public class RceDemo {
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println(1);
Process process = Runtime.getRuntime().exec("cmd /c notepad");
process.waitFor();
System.out.println(2);
}
}
3.2 cmd /k notepad
3.3 cmd /c start notepad
3.4 cmd /k start notepad
4 参考资料
更多推荐



所有评论(0)