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

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

问题描述

目前,我正在试图调用从C ++一些Java code使用JNI Android应用程序。但是,我不能得到任何东西,当我试图创建一个使用JNI_CreateJavaVM方法的Java虚拟机进行编译。它配备了错误:未定义的参考`JNI_CreateJavaVM'

这显然将jni.h头文件中声明,我能够利用类型以及没有错误的头文件中做结构定义的,因此code肯定是包括的。这只是无力的时候我试图利用JNI_CreateJavaVM来编译。是否有需要与它包括沿,还是有越来越虚拟机时调用Java从C ++?

一些其他的方法,别的东西

下面是code我试图与构建它:

 的#includeHelloWorldScene.h
#包括< stdio.h中>
#包括< jni.h>
#包括< string.h中>


布尔的HelloWorld ::的init()
{
    JavaVM的* JVM;
    JNIEnv的* ENV;
    JavaVMInitArgs ARGS;
    jint结果= JNI_CreateJavaVM(安培; JVM,和放大器; ENV(无效*)及参数); //将code编译如果该行被注释掉。

    //...Various初始化程序

    返回true;
}
 

解决方案

这是一个链接错误,不是编译错误。你需要对JVM库进行链接。

假设你正在使用GCC,这将是这样的:

  -L /路径/到/的Java / JRE / LIB /<弓> /<服务器或客户端> -ljvm
 

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

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

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