如何从 C++ 调用 Java 函数? [英] How to call Java functions from C++?

查看:44
本文介绍了如何从 C++ 调用 Java 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 C++ 应用程序调用 Java 函数?

How can I call Java functions from a C++ application?

我知道从 CMD(或类似技术)调用它们,但我宁愿不使用它们.

I know about calling them from CMD (or similar techniques), but I would rather not use them.

推荐答案

从 C 创建一个 JVM.它显示了创建 JVM 和调用方法的示例过程.如果JVM已经存在;例如您的 C 程序被 Java 程序调用(回调情况),您可以缓存 JNIEnv* 指针.

As an example, check Creating a JVM from C. It shows a sample procedure to create a JVM and invoke a method. If the JVM already exists; e.g. your C program is invoked by the Java program (callback situation), you can cache the JNIEnv* pointer.

作为一个建议,从 C/C++ 缓存指向 JVM 的指针时要小心,有一些语义涉及您可以缓存的内容,并且可以稍后调用.为此,正如 Brian Agnew 所指出的,请查看 JNI 参考.

As an advice, be careful caching pointers to the JVM from C/C++, there are some semantics involved as to what you can cache and it could be invoked later on. For that, as Brian Agnew pointed out, check the JNI reference.

这篇关于如何从 C++ 调用 Java 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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