Redirect System.err for a single thread

Is it possible to redirect System.err for a single thread?

I have a servlet that kicks off javac. I save the System.err PrintStream and I redirect System.err to an output file during the compile. After the compile is complete, I redirect System.err back to it's original destination and read the file if the compile did not complete successfully.

Unfortunately, I believe there is a small hole here if another thread wants to write to System.err during the time a compile is taking place and I may end up with these messages in my compile output file. What I would really like to do is only redirect System.err for the thread that the compile is taking place on. Does anyone know if this is possible? Thanks.

[736 byte] By [jburggraf] at [2007-9-26 1:18:04]
# 1

I don't think there is a way to do that. But you could just make an OutputStream object in each of your classes called "err". Set "err" equal to System.err in all your classes except the one class you wish to override.

Then you have to change all your statements to :

err.println(...)

instead of System.err.println(...)

Sorry, this is the only way I know how to do this and it's not the answer you're looking for.

schillj at 2007-6-29 0:48:34 > top of Java-index,Core,Core APIs...
# 2

Write your own PrintStream. It takes the old System.err and a String name.

You create one and set it using System.setErr()

When the write method(s) of your PrintStream are call it checks the Thread.currentThread().getName() to see if is the same as the passed in name. If yes then do something, if not then call the old System.err.

jschell at 2007-6-29 0:48:34 > top of Java-index,Core,Core APIs...
# 3
Thanks for your help. I ended up doing something similar to your suggestion.
jburggraf at 2007-6-29 0:48:34 > top of Java-index,Core,Core APIs...