简体   繁体   English

如何使用JNI在C ++中使用Java类?

[英]How can I use JNI in C++ to use a Java class?

so I'm trying to use JNI to call my Java class in C++ and everything looks well until I try to run it. 因此,我尝试使用JNI在C ++中调用Java类,在尝试运行它之前,一切看起来都不错。 In Xcode I get the error 在Xcode中,我得到了错误

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? 我认为这与体系结构有关,但是不知道如何解决,有什么帮助吗?

The code I use to initialize the Java VM is 我用来初始化Java VM的代码是

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

I'm on a mac, please post a mac solution if you have any ideas, I'm trying to avoid loading libraries at runtime. 我在Mac电脑上,如果您有任何想法,请发布Mac解决方案,我试图避免在运行时加载库。 Thanks 谢谢

It sounds like you're missing a library. 听起来您好像缺少图书馆。 If you're using gcc check all your -l 's. 如果您使用的是gcc,请检查所有-l If you're using VS, check your "Additional Dependancies" options under Config->Linker->Input. 如果使用的是VS,请在“配置”->“链接器”->“输入”下检查“其他依赖项”选项。

Also check that you have the x86_64 version of the necessary libraries. 还要检查您是否具有必需库的x86_64版本。

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. 这是在Windows上的操作方法。 Don't know the syntax for doing it on other operating systems. 不知道在其他操作系统上执行此操作的语法。

[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] 这可能会编译,也可能不会编译,但它应该可以让您入门]

First create your own typedef for a pointer to the function 首先为您的函数指针创建自己的typedef

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

Look up the JVM dll using LoadLibrary . 使用LoadLibrary查找JVM dll。 It's up to your application to figure out where to find the JVM DLL. 由您的应用程序确定在哪里可以找到JVM DLL。 In our case we distributed a 3rd party JRE and knew where to expect the DLL. 在我们的案例中,我们分发了第三方JRE,并且知道在哪里可以找到DLL。

HMODULE jvmDll = LoadLibrary(jvmDllPath);

Next lookup the address of the function from the JVM dll using GetProcAddress 接下来,使用GetProcAddress从JVM dll中查找函数的地址

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

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);

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 :) 现在您要做的就是在代码找不到DLL时处理运行时错误:)

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM