gpt4 book ai didi

java - 来自 C++ 的 JNI Java;打印输出?

转载 作者:太空宇宙 更新时间:2023-11-04 15:44:01 25 4
gpt4 key购买 nike

我正在使用 JNI 在 C++ 程序中调用一些 Java 代码。我需要将一些文本从 Java 打印到 C++ 标准输出。

我该怎么做?

我尝试:System.out.println("sdf"); 在 java 中,没有出现。

请帮忙:D

最佳答案

public class Natives {
public static native void printf(final String WhatToPrintHere);
}

public class Main {
public static void main(String args[]) {
Natives.printf("Testing printing from Java");
}
}


extern "C" JNIEXPORT void Java_Natives_printf(JNIEnv* env, jobject obj, jstring WhatToPrintHere)
{
const char* Str = env->GetStringUTFChars(WhatToPrintHere, 0);

std::cout<< Str <<"\n";

env->ReleaseStringUTFChars(WhatToPrintHere, Str);
}

关于java - 来自 C++ 的 JNI Java;打印输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19548055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com