Obteniendo respuesta de la línea de comando en 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.

Muchas Gracias

preguntado el 09 de marzo de 12 a las 14:03

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

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! -

2 Respuestas

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)

Si ejecuta:

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

Con la Process Puedes llamar

InputStream errorStream =  process.getErrorStream();

You will capture the output from the bat to stderr.

También puedes usar getOutputStream() on process to get sdtout or check the return code with exitCode().

respondido 09 mar '12, 16:03

The ProcessBuilder method is asking for a String Array, if I use the bat file there it won't work. - danny gloudemans

@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. - 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

respondido 09 mar '12, 15:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.