I am invoking a C library via JNI that prints to stdout. How can I redirect this output to System.out?
我通过JNI调用一个C库,打印到stdout。如何将此输出重定向到System.out?
2 个解决方案
#1
8
System.out
is stdout
. Is there some more fundamental problem you're having (mixed up output, perhaps?).
System.out是stdout。你有一些更基本的问题(混合输出,也许?)。
Since another member has also mentioned that last point - I should explain further:
由于另一位成员也提到了最后一点 - 我应该进一步解释:
System.out
and stdout
both correspond to file descriptor #1.
System.out和stdout都对应于文件描述符#1。
However both Java's OutputStream
(and derived classes) and C's stdio
library have their own (independent) buffering mechanisms so as to reduce the number of calls to the underlying write
system call. Just because you've called printf
or similar, it's not guaranteed that your output will appear straightaway.
但是,Java的OutputStream(和派生类)和C的stdio库都有自己的(独立的)缓冲机制,以减少对底层写入系统调用的调用次数。只是因为你已经调用了printf或类似的东西,所以不能保证你的输出会直接出现。
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.
因为这些缓冲方法是独立的,所以Java代码中的输出可能(理论上)混淆或者相对于C代码的输出无序地出现。
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 using stdio
rather than the low-level write
call) you should call fflush(stdout)
before returning.
如果这是一个问题,你应该安排在调用你的JNI函数之前调用System.out.flush(),并且在你的C函数中(如果它使用stdio而不是低级写调用)你应该在之前调用fflush(stdout)返回。
#2
1
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.
正如Alnitak所写,你应该打印到stdout。您应该注意,消息可能需要一段时间才会显示在屏幕上。如果时间很重要,您应该在打印到stdout时打印带有消息的时间戳。
#1
8
System.out
is stdout
. Is there some more fundamental problem you're having (mixed up output, perhaps?).
System.out是stdout。你有一些更基本的问题(混合输出,也许?)。
Since another member has also mentioned that last point - I should explain further:
由于另一位成员也提到了最后一点 - 我应该进一步解释:
System.out
and stdout
both correspond to file descriptor #1.
System.out和stdout都对应于文件描述符#1。
However both Java's OutputStream
(and derived classes) and C's stdio
library have their own (independent) buffering mechanisms so as to reduce the number of calls to the underlying write
system call. Just because you've called printf
or similar, it's not guaranteed that your output will appear straightaway.
但是,Java的OutputStream(和派生类)和C的stdio库都有自己的(独立的)缓冲机制,以减少对底层写入系统调用的调用次数。只是因为你已经调用了printf或类似的东西,所以不能保证你的输出会直接出现。
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.
因为这些缓冲方法是独立的,所以Java代码中的输出可能(理论上)混淆或者相对于C代码的输出无序地出现。
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 using stdio
rather than the low-level write
call) you should call fflush(stdout)
before returning.
如果这是一个问题,你应该安排在调用你的JNI函数之前调用System.out.flush(),并且在你的C函数中(如果它使用stdio而不是低级写调用)你应该在之前调用fflush(stdout)返回。
#2
1
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.
正如Alnitak所写,你应该打印到stdout。您应该注意,消息可能需要一段时间才会显示在屏幕上。如果时间很重要,您应该在打印到stdout时打印带有消息的时间戳。