Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting response of command line in Java

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

like image 757
Danny Gloudemans Avatar asked Mar 09 '12 14:03

Danny Gloudemans


2 Answers

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().

like image 200
rogermushroom Avatar answered Oct 21 '22 21:10

rogermushroom


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

like image 40
Alex Avatar answered Oct 21 '22 21:10

Alex