在我的Windows系统上,我想使用Runtime.getRuntime().exec(command)
启动带有python脚本的子进程,并打开命令提示符终端,以便用户可以看到该进程正在运行。我的命令是这样的:
val command = "cmd /c python ~path_to_file~ ~args~"
我知道还有另一种方法可以通过以下方式将命令提示符的内容打印回原始终端:
import java.util.Scanner
fun main(args: Array<String>) {
val proc = Runtime.getRuntime().exec("cmd /C dir")
Scanner(proc.inputStream).use {
while (it.hasNextLine()) println(it.nextLine())
}
}
只是想知道我还有没有其他选择。
我认为您应该使用ProcessBuilder
的重定向:
fun main() {
ProcessBuilder("cmd", "/C", "dir")
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.start()
.waitFor()
}
此示例与您的行为相同。