How do I get the return code in Java after executing a windows command line command

前端 未结 4 1379
渐次进展
渐次进展 2021-01-12 05:17

I\'m doing something like this in Java right now

Process p = Runtime.getRuntime().exec(\"ping -n 1 -w 100 127.0.0.1\")

How can I read the w

相关标签:
4条回答
  • 2021-01-12 05:47

    Use Process.exitValue() method. You will need to handle the exception thrown if the process has not yet exited and retry.

    Or, you could use Process.waitFor() to wait for the process to end and it will return the process exit value also (thanks to increment1).

    0 讨论(0)
  • 2021-01-12 05:52

    You can use Process#exitValue()

    0 讨论(0)
  • 2021-01-12 05:59

    You

    waitFor()
    it and then get
    exitValue()
    .

    0 讨论(0)
  • 2021-01-12 06:07

    next line of code:

    int returnCode = p.waitFor();
    

    This blocks until process is complete. You can also use the Process.exitValue() method, if you don't want to block. See Java6 Process class API doc

    0 讨论(0)
提交回复
热议问题