How to capture the exit status of a shell command in Java?

我与影子孤独终老i 提交于 2019-12-05 04:35:46

You're looking for Process#exitValue

String command = "diff "+mp.get("directory")+"/"+fileName+" "+mp.get("outdir")+"/"+fileName;
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
int exitStatus = p.exitValue();

Don't forget, you should read the contents of the InputStream even if you don't care, some processes will choke (not finish) until the output buffer has been read...

You can try using ProcessBuilder class to create a Processobject, whose exitValue() should help you.

Use method waitFor() of class Process. It returns an int, the return value of the process.

Runtime.exec() and friends return a Process object, which has an exitValue() method that returns the exit code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!