1

To capture the output of process in Groovy I use the following:

"command".execute().text

I want to do the same in Java, but all responses I found contain a lot of boilerplate code involving loops, BufferedReader, Scanner, etc.:

Can I do the same thing within 1-2 lines of code? Maybe Guava or Apache have something to make life simpler?

Community
  • 1
  • 1
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
  • Java is roughly synonymous with boilerplate code. In this case, there might be a lib (probably is) that will help, but much of Java is going to be boilerplate. I'm fairly sure you can do this in like . . . 5-10 lines though. – CollinD Feb 07 '16 at 00:18
  • There is [Apache Commons Exec](https://commons.apache.org/proper/commons-exec/), I've never used it, so I don't know how good it is. It is a problem that comes up so infrequently that it's probably not worth it. I wrote my own ten-line process runner a few years ago, and I've been happily using it since. – biziclop Feb 07 '16 at 00:19
  • if you have access to the groovy jar in your project, you can call getText() and have the same result – Jérémie B Feb 07 '16 at 00:31
  • @CollinD the thing here is that I need to use it once in one unit test, so these 5-10 lines would be more than a test itself. – Michal Kordas Feb 07 '16 at 00:55

1 Answers1

-1

I've managed to come up with the following two-liner:

Process process = Runtime.getRuntime().exec("command")'
String output = IOUtils.toString(process.getInputStream());
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
  • if your process use stderr, you can block – Jérémie B Feb 07 '16 at 00:38
  • @JérémieB can you explain that scenario? What do you mean by "you can block"? – Michal Kordas Feb 07 '16 at 00:42
  • Did you mean process.getOutputStream() ? your question was how to capture output. – SomeDude Feb 07 '16 at 00:44
  • if the process write to the error stream, and you don't read it, then the standard buffer will be full, and the process will block waiting some space in this buffer. as a consequence, your code will block, waiting the process to be terminated. the streams of the process must be "pumped". – Jérémie B Feb 07 '16 at 00:45
  • Did you exactly look at toString() on IOUtils, toString() takes InputStream, I don't see any method that takes "OutputStream" – SomeDude Feb 07 '16 at 00:48
  • Process.getInputStream() return the output of the process – Jérémie B Feb 07 '16 at 00:49
  • @JeremieB interesting indeed. But then majority of other SO answers have the same but anyway. – Michal Kordas Feb 07 '16 at 00:51
  • This is a bad idea. As @JérémieB points out, if the process writes to stderr your program will block. A lot of procsses write to stderr when they encounter errors. Your program will simply block in those cases and you have no idea what has happened. – Jon Tirsen Sep 01 '17 at 07:35
  • @JonTirsen you are right. Can you propose the correct solution? I'll accept it when proved to work. – Michal Kordas Sep 08 '17 at 11:38