我如何重定向从C程序用JNI来的System.out端子输出? [英] How do I redirect terminal output from a C program to System.out with JNI?

查看:305
本文介绍了我如何重定向从C程序用JNI来的System.out端子输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用通过JNI一个C库,打印到stdout。我这怎么能输出重定向到System.out?

I am invoking a C library via JNI that prints to stdout. How can I redirect this output to System.out?

推荐答案

的System.out 标准输出。有没有您遇到(混合起来的输出,也许?)一些更根本的问题。

System.out is stdout. 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 标准输出都对应于文件描述符#1。

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

不过无论Java的的OutputStream (和派生类)和C的 STDIO 图书馆有自己的(独立的)缓冲机制从而减少调用底层系统调用的次数。仅仅因为你叫的printf 或类似的,它不能保证你的输出就会出现通俗易懂。

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 code内的输出可以(在理论上)混合起来或以其他方式显示与输出从C code外的顺序。

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.

如果这是一个问题,你应该安排调用 System.out.flush()如果它使用致电JNI函数之前,以及在C函数( STDIO ,而不是低层次的调用),你应该叫 fflush(标准输出)返回前。

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.

这篇关于我如何重定向从C程序用JNI来的System.out端子输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆