简体   繁体   中英

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 "JNI_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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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