Apps
Apps

Reputation: 23

How do we execute Batch files from Java

I need to execute a batch file from 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?

Upvotes: 0

Views: 3154

Answers (4)

Pablo Grisafi
Pablo Grisafi

Reputation: 5047

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.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168845

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

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

Upvotes: 2

Simon G.
Simon G.

Reputation: 6717

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.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86509

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.

Upvotes: 3

Related Questions