I am invoking a function that is printing some string in my console/standard output. I need to capture this string. I cannot modify the function that is doing the printing, nor change runtime behavior through inheritance. I am unable to find any pre-defined methods that will allow me to do this.
Does the JVM store a buffer of printed contents?
Does anyone know of a Java method that will aid me?
You could temporarily replace System.err or System.out with a stream that writes to string buffer.
You can redirect the standard output by calling
Or - if you need to log it at runtime, pipe the output to a file:
Another trick - if you want to redirect and can't change the code: Implement a quick wrapper that calls your application and start that one: