How to redirect terminal output from program C to System.out with JNI?

I call the C library through the JNI, which prints to stdout. How to redirect this output to System.out?

+4
source share
2 answers

System.out stdout Is there another fundamental problem that you are facing (maybe mixing output?).

Since the other member also mentioned the last point - I have to explain further:

System.out and stdout both correspond to file descriptor # 1.

However, the Java libraries OutputStream (and derived classes) and C stdio have their own (independent) buffering mechanisms to reduce the number of calls to the base write system call. Just because you called printf or the like does not guarantee that your output will appear immediately.

Since these buffering methods are independent, output from Java code may (theoretically) be confused or otherwise out of order with respect to output from C.

If this is a concern, you should arrange to call System.out.flush() before calling the JNI function and in your C function (if it uses stdio rather than a low level write call), you should call fflush(stdout) before returning.

+8
source

As Alnimak wrote, you should print to stdout. You should notice that a message may appear on the screen. If timing is important, you should print a time stamp with a message when printing to standard output.

+1
source

All Articles