Wiz
Wiz

Reputation: 38

Java find out if Batch file ran successfully

I'm running a Batch file from Java like so:

Runtime
    .getRuntime()
    .exec("cmd /c " + filepath);

Is there any way to find out if the Batch file ran successfully or failed from within Java?

Upvotes: 1

Views: 208

Answers (1)

Cardinal System
Cardinal System

Reputation: 3422

Use the Process instance that Runtime#exec(String) constructs:

Process p = Runtime.getRuntime().exec("cmd /c " + filepath);

You can call Process#waitFor which "causes the current thread to wait, if necessary, until the process represented by this Process object has terminated." Afterwords, see if it completed successfully with Process#exitValue.

You can also interact with the process by fetching its input and output streams:

InputStream inputStream = p.getInputStream(), errorStream = p.getErrorStream();
OutputStream outputStream = p.getOutputStream();

Upvotes: 2

Related Questions