Question: I have a big program developed in C, and most functions have several
`fprintf(stderr, "message")` lines.

But right now I need to use those functions in a Java program. So, I implement a JNI interface to communicate between Java and C. Everything is ok, Java and C can communicate, but now I want to capture the several stderr's of C in Java... How can I do that?

Cheers ;)

Answer: 
Out of my experience exactly with the same problem, i can tell that JNI bridge has very strange opinion on the importance of what's going out of JNI through standard streams. Generally it does heavy caching, spilling in unpredictable chunks and timing, and we didn't figure out how to force flushing.

We ended up with mass regex replacement of `*printf()` statements with logging to a file, done altogether on C side. Tailing the resulting logfile was way more reliable than any attempts to convince JVM that you really need the logging when C layer says so, not when JVM makes its mind on the buffers.
When you call a JNI library, you are loading it into the JVM process, so the library STDOUT and STDERR will be the JVM STDOUT and STDERR. So the question is OS specific, and is more like "How can a process read its own STDOUT/STDERR?"

There are a couple of possibilities [Internally capture/redirect stdout?](https://stackoverflow.com/questions/7286918/internally-capture-redirect-stdout), but what I don't see easy is reading ONLY the STDOUT or STDERR of your library (I think that with the example I give you, once you perform the redirection you cannot go back to use console for output (\*)).

As commented, the answer will depend heavily of which is your OS.

(\*) I may be wrong, I am not so proficient at C/system programming.