在Android应用程序中从c ++调用Java代码 [英] Calling Java code from c++ in an Android application

查看:135
本文介绍了在Android应用程序中从c ++调用Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正尝试在使用JNI的Android应用程序中从C ++调用一些Java代码。但是,当我尝试使用JNI_CreateJavaVM方法创建一个java虚拟机时,我无法编译任何东西。它出现的错误:未定义的引用`JNI_CreateJavaVM'

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

它清楚地在jni.h头文件中声明,我能够使用类型和在头文件中做出的结构定义没有错误,所以代码绝对包括它。它只是无法编译时,我试图使用JNI_CreateJavaVM。是否还有其他的东西需要包括在一起,还是有其他方法让一个虚拟机从C ++调用Java?

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


推荐答案

编译错误。您需要链接到JVM库。

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

假设您正在使用GCC,那将是:

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

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

这篇关于在Android应用程序中从c ++调用Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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