使用JNI在QT中从jar文件中查找java类

时间:2016-08-16 10:18:00

标签: java c++ qt java-native-interface

我创建了一个简单的java类并导出为jar文件。

然后我尝试使用JNI在C ++中加载java类,但我不能。

但是,如果我直接从java文件中加载它,那么从jar文件开始,它就没有了。

Pdf.java

package PdfCore;

public class Pdf {

    public int x,y;
    public Pdf(int _x, int _y){
        x = _x;
        y = _y;
    }
    public int sum(){
        return x+y;
    }

}

Test.pro

TEMPLATE = app

QT += qml quick
CONFIG += c++11

SOURCES += main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

DISTFILES += \
    android/AndroidManifest.xml \
    android/gradle/wrapper/gradle-wrapper.jar \
    android/gradlew \
    android/res/values/libs.xml \
    android/build.gradle \
    android/gradle/wrapper/gradle-wrapper.properties \
    android/gradlew.bat \
    android/src/XPdf.jar

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

的main.cpp

#include <jni.h>
#include <QDebug>

static JavaVM* s_javaVM = 0;
static jclass s_XPdfID = 0;
static jmethodID s_XPdfConstructorMethodID=0;
static jmethodID s_XPdfSumMethodID=0;
jobject m_XPdfObject;

JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*)
{
    JNIEnv* env;

    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
        qCritical()<<"Can't get the enviroument";
        return -1;
    }

    s_javaVM = vm;
    jclass clazz=env->FindClass("PdfCore/Pdf");
    if (!clazz)
    {
        qCritical()<<"Can't find Pdf class";
        return -1;
    }
    return JNI_VERSION_1_6;
}

jni.h文件取消定义

#ifdef __cplusplus
extern "C" {
#endif
/*
 * VM initialization functions.
 *
 * Note these are the only symbols exported for JNI by the VM.
 */
#if 0  /* In practice, these are not exported by the NDK so don't declare them */
jint JNI_GetDefaultJavaVMInitArgs(void*);
jint JNI_CreateJavaVM(JavaVM**, JNIEnv**, void*);
jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*);
#endif

0 个答案:

没有答案