5

I don't know if it is possible, but I want to see the response of my .bat file in Java.

Or a boolean of something that I can see that everything were going well! And if there where some errors that I can see the errors in Java.

Thanks

2
  • Your question is pretty unclear. Are you talking about just printing to the console from your Java program? (use System.out.println()). I'm not sure where the .bat file comes into it. Commented Mar 9, 2012 at 14:58
  • Sorry, I was running the .bat file in the Windows Command Line. But I'm building a program for users, So it is not the best way to run the bat file only and the users don't get any feedback. I hope it is now more clear! Commented Mar 9, 2012 at 15:23

2 Answers 2

5

You need to run it using the ProcessBuilder (as long as you are running java 1.5 or above, if not check Alex's answer)

If you execute:

Process process = new ProcessBuilder("mybat.bat").command();

With the Process you can call

InputStream errorStream =  process.getErrorStream();

You will capture the output from the bat to stderr.

You can also use getOutputStream() on process to get sdtout or check the return code with exitCode().

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

2 Comments

The ProcessBuilder method is asking for a String Array, if I use the bat file there it won't work.
@Gynnad What do you mean use the bat file? I am passing a string describing the bat file (either the path of the bat file or if it is on the system path then just the bat file name) It is described well in the documentation for ProcessBuilder (link provided in the answer). If you are using java 1.5 or greater you should be using ProcessBuilder.
2

Take a look at these code samples for executing shell commands through java: Execute an external program

The code examples above do not utilize ProcessBuilder and therefore are not limited to Java 1.5 and above

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.