开发者

How do we execute Batch files from Java

I need to execute a batch file fr开发者_开发问答om my Java Program. I found multiple threads related to this query. Execute Batch File from Java

In addition to the above information, I need to know if that operation was executed successfully or not. Is it possible to get a handle to that from Java?


Both Runtime.exec() and ProcessBuilder.start() return a Process object.

With that, you can use Process.getExitValue(). That said, I don't happen to know if the shell's exit value is the same as the script's.


You should check out When Runtime.exec() won't.

I highly recommend it. It will probably answer your next 4-5 questions.


Use java.lang.ProcessBuilder to create the call to the batch file. The Process object will allow you to monitor the output and the exit code from the batch file. A non-zero exit code typically indicates failure.


You should really check Commons Exec. It will help you getting the output from the batch file, and setting a timeout, and even creating the command line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜