共計(jì) 393 個(gè)字符,預(yù)計(jì)需要花費(fèi) 1 分鐘才能閱讀完成。
Java 中可以通過(guò) Runtime
類或者 ProcessBuilder
類來(lái)調(diào)用 shell 命令。
- 使用
Runtime
類調(diào)用 shell 命令:
String command = "ls -l"; // 要執(zhí)行的 shell 命令
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command); // 執(zhí)行命令
- 使用
ProcessBuilder
類調(diào)用 shell 命令:
String command = "ls -l"; // 要執(zhí)行的 shell 命令
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start(); // 執(zhí)行命令
以上代碼都是執(zhí)行了 ls -l
命令,并將結(jié)果輸出到控制臺(tái)。你可以根據(jù)自己的需求修改要執(zhí)行的 shell 命令。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完