Java/Kotlin 运行 Runtime.getRuntime().exec() 带有可见的命令提示符

Java/Kotlin run Runtime.getRuntime().exec() with visible command prompt

在我的 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()
}

此示例与您的行为相同。