I am invoking a C library via JNI that prints to stdout. How can I redirect this output to System.out?
-
System.out
isstdout
. Is there some more fundamental problem you're having (mixed up output, perhaps?).Since another member has also mentioned that last point - I should explain further:
System.out
andstdout
both correspond to file descriptor #1.However both Java's
OutputStream
(and derived classes) and C'sstdio
library have their own (independent) buffering mechanisms so as to reduce the number of calls to the underlyingwrite
system call. Just because you've calledprintf
or similar, it's not guaranteed that your output will appear straightaway.Because these buffering methods are independent, output from within Java code could (in theory) get mixed up or otherwise appear out-of-order relative to output from the C code.
If that's a concern, you should arrange to call
System.out.flush()
before calling your JNI function, and in your C function (if it's usingstdio
rather than the low-levelwrite
call) you should callfflush(stdout)
before returning. -
As Alnitak wrote, you should print to stdout. You should note that it can take a while for the message to appear on the screen. In case the timing is important, you should print a timestamp with the message when you print to stdout.
0 comments:
Post a Comment