开发者

Calling Java code from c++ in an Android application

I'm currently attempting to call some Java code from C++ in an Android application using JNI. However, I cannot get anything to compile when I attempt to create a java virtual machine using the "JN开发者_JS百科I_CreateJavaVM" method. It comes up with the error: "undefined reference to `JNI_CreateJavaVM'"

It's clearly declared in the jni.h header file, and I am able to make use of type and struct definitions that are made in the header file without error, so the code is definitely including it. It's just unable to compile when I attempt to make use of JNI_CreateJavaVM. Is there something else that needs to be included along with it, or is there some other method of getting a virtual machine to make calls to Java from C++?

Here is the code I'm attempting to build it with:

#include "HelloWorldScene.h"
#include <stdio.h>
#include <jni.h>
#include <string.h>


bool HelloWorld::init()
{
    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs args;
    jint result = JNI_CreateJavaVM(&jvm, &env, (void*)&args);//The code compiles if this line is commented out.

    //...Various initialization procedures

    return true;
}


That's a linker error, not a compile error. You need to link against the JVM library.

Assuming you're using GCC, that would be something like:

-L/path/to/java/jre/lib/<arch>/<server or client> -ljvm
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜