开发者

Catching and modifying stderr sent from external JAR

Is there a way for me to intercept the STDERR stream that is being used from an external JAR file?

My situation is that I have my own program using STDERR for XYZ, and an external JAR that uses STDERR for ABC. I want to merge both so that they are formatted correctly for the end user, but I cannot figure out how to catch 开发者_运维技巧or redirect or (what is the right term?) the JAR's STDERR?

Thanks,


The standard error stream in java programs is System.err and standard output stream is System.out.

You can override these both using the System.setErr(java.io.PrintStream) and System.setOut to a PrintStream object. But this will be replaced globally. Provide an implementation of a PrintStream that will do the formatting and set this as an standard error stream for both.

If you have two different processes, then you can get another processes's error stream (as an input stream) read content from it (fromat it) and write it to your standard error stream.


Not an easy solution, but you could create a classloader, java agent or preprocessor that modifies the bytecode of selected classes and replaces all references to the static field System.err to access your custom output stream.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜