JNI标准输出不会打印到控制台

时间:2022-02-11 00:06:42

I use JNI because I need some c libraries. My problem is that not all is printed to the stdout. I know I can do

我使用JNI是因为我需要一些c库。我的问题是并非所有都打印到标准输出。我知道我能做到

puts("test");
fflush(stdout);

by myself. But my problem is that the c library has a debug function which prints to the stdout and therefore is not visible in the console. How can I solve this?

我自己。但我的问题是c库有一个调试功能,它打印到stdout,因此在控制台中不可见。我怎么解决这个问题?

1 个解决方案

#1


You cannot write to stdout because JNI runs the native code from within the Java code, and is not attached to the stdout. You can use the following function to write directly to the console device.

您无法写入stdout,因为JNI从Java代码中运行本机代码,并且未附加到stdout。您可以使用以下函数直接写入控制台设备。

void console_printf(const char *fmt,...)
{
    int fd = open("/dev/console", O_WRONLY);
    char buffer[1000];
    if (fd < 0)
        return;

    va_list ap;
    va_start(ap, fmt);
    vsprintf(buffer, fmt, ap);
    va_end(ap);

    write(fd, buffer, strlen(buffer));
    close(fd);
}

#1


You cannot write to stdout because JNI runs the native code from within the Java code, and is not attached to the stdout. You can use the following function to write directly to the console device.

您无法写入stdout,因为JNI从Java代码中运行本机代码,并且未附加到stdout。您可以使用以下函数直接写入控制台设备。

void console_printf(const char *fmt,...)
{
    int fd = open("/dev/console", O_WRONLY);
    char buffer[1000];
    if (fd < 0)
        return;

    va_list ap;
    va_start(ap, fmt);
    vsprintf(buffer, fmt, ap);
    va_end(ap);

    write(fd, buffer, strlen(buffer));
    close(fd);
}