Java Runtime.getRuntime(): getting output from executing a command line program

前端 未结 12 1365
清歌不尽
清歌不尽 2020-11-22 00:18

I\'m using the runtime to run command prompt commands from my Java program. However, I\'m not aware of how I can get the output the command returns.

Here is my code:

12条回答
  •  广开言路
    2020-11-22 00:39

    If you write on Kotlin, you can use:

    val firstProcess = ProcessBuilder("echo","hello world").start()
    val firstError = firstProcess.errorStream.readBytes().decodeToString()
    val firstResult = firstProcess.inputStream.readBytes().decodeToString()
    

提交回复
热议问题