0

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?

0

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that. But I'm getting the following exceptin.Exception in thread "main" java.lang.IllegalThreadStateException: process has not exited at java.lang.ProcessImpl.exitValue(Native Method) at com.mt.test.TestClass2.main(TestClass2.java:13)
I think this thread should solve my problem stackoverflow.com/questions/2448402/…
Check out download.oracle.com/javase/1.5.0/docs/api/java/lang/… Specifically, the method waitFor().
2

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

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

Comments

0

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.

1 Comment

I think this thread should solve my problem stackoverflow.com/questions/2448402/…
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.