执行windows命令行命令后,如何在Java中获取返回码 [英] How do I get the return code in Java after executing a windows command line command
问题描述
我现在用Java做这样的事情
I'm doing something like this in Java right now
Process p = Runtime.getRuntime().exec("ping -n 1 -w 100 127.0.0.1")
如何读取windows exec代码?我已经知道如何从命令中读取命令行输出,但如果我只想让0或1告诉我它是成功还是失败怎么办?
How can I read the windows exec code? I already know how to read the command line output from the command, but what if I just want the 0 or 1 telling me whether it was successful or failed?
推荐答案
使用 Process.exitValue()
方法。如果进程尚未退出并重试,则需要处理抛出的异常。
Use Process.exitValue()
method. You will need to handle the exception thrown if the process has not yet exited and retry.
或者,您可以使用 Process.waitFor()
等待进程结束,它也将返回进程退出值(感谢 increment1 )。
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).
这篇关于执行windows命令行命令后,如何在Java中获取返回码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!