如何使用JNI在C ++中使用的Java类? [英] How can I use JNI in C++ to use a Java class?

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

问题描述

所以我试图用JNI调用我的Java类在C ++中,一切看起来很好,直到我尝试运行它。在X $ C $三我得到的错误

so I'm trying to use JNI to call my Java class in C++ and everything looks well until I try to run it. In Xcode I get the error

Undefined symbols for architecture x86_64: "_JNI_CreateJavaVM", referenced from:

我以为有什么做的架构,但不知道如何解决这个问题,任何帮助?

Which I assume has something to do with the architecture, but don't know how to fix this, any help?

在code我用初始化Java虚拟机是

The code I use to initialize the Java VM is

JNI_CreateJavaVM(&internal::gJVM, 
(void**)&internal::gEnv, &vm_args);

我在Mac上,请张贴MAC解决方案,如果您有任何想法,我想,以避免加载库在运行时。谢谢

I'm on a mac, please post a mac solution if you have any ideas, I'm trying to avoid loading libraries at runtime. Thanks

推荐答案

在我见过的所有例子,他们避免这个问题从来没有被直接调用该函数,因此,它从来没有链接,因此没有错误。诀窍是在运行时查找功能,并通过函数指针调用。这里是如何做到这一点在Windows上。不知道语法做其他操作系统上。

In all the examples I have seen, they avoid the problem by never invoking that function directly, thus it never has to link, thus no error. The trick is to lookup the function at runtime and invoke via function pointer. Here is how to do it on Windows. Don't know the syntax for doing it on other operating systems.

[免责声明 - 我复制从几个功能/粘贴code和可能引入了编译器错误。这可能会或可能不会进行编译,但它应该让你开始]

[Disclaimer - I copied/pasted code from several functions and may have introduced compiler errors. This may or may not compile but it should get you started]

首先创建自己的指针的typedef功能

First create your own typedef for a pointer to the function

typedef jint (JNICALL* JvmCreateProcTypeDef)(JavaVM **, void **, void);

使用的调用LoadLibrary 。这取决于你的应用程序,以找出其中找到JVM DLL。在我们的例子中,我们分发的第三方JRE和知道在哪里,期望DLL。

Look up the JVM dll using LoadLibrary. It's up to your application to figure out where to find the JVM DLL. In our case we distributed a 3rd party JRE and knew where to expect the DLL.

HMODULE jvmDll = LoadLibrary(jvmDllPath);

接着查找函数的从JVM的DLL使用地址 GetProcAddress的

JvmCreateProcTypeDef jvmCreateProc = (JvmCreateProcTypeDef) GetProcAddress(jvmDll,"JNI_CreateJavaVM");

现在更换您的code直接调用功能,像它通过函数指针调用它的以下内容:

Now replace your code which calls the function directly, to something like the following which calls it via function pointer:

jvmCreateProc(&internal::gJVM, (void**)&internal::gEnv, &vm_args);

这应该让你通过了所有的编译链接错误。现在,所有你需要做的就是处理了运行时错误时,你的code找不到DLL:)

This should get you passed all the compile link errors. Now all you have to do is deal with the runtime errors when your code cannot find the DLL :)

希望这有助于!

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

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