Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture C stderr from Java JNI

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 ;)

like image 553
josecampos Avatar asked Nov 15 '12 00:11

josecampos


1 Answers

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.

like image 124
Pavel Zdenek Avatar answered Oct 23 '22 03:10

Pavel Zdenek