从C ++调用非静态java方法时的JNI访问冲突 [英] JNI access violation when calling non static java method from C++

查看:439
本文介绍了从C ++调用非静态java方法时的JNI访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从c ++调用非静态java方法。



Sample2.java:

  public class Sample2 {
public int intMethod(int n){
return n * n;
}
}

JNITest.cpp:

  #includestdafx.h
#include< iostream>
#include< string>
#include< memory.h>
#include< jni_md.h>
#include< jni.h>
using namespace std;

#ifdef _WIN32
#define PATH_SEPARATOR';'
#else
#define PATH_SEPARATOR':'
#endif

int _tmain(int argc,_TCHAR * argv [])
{
JavaVMOption options [3];
static JNIEnv * env;
JavaVM * jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls,stringClass;
jmethodID mid;
jstring jstr;
jobjectArray args;

jint square;

options [0] .optionString =-Djava.class.path = D:\\Studie\\EXP\\ Code\\Workspace\\JNItest \\bin; // 2APL\\build; // Workspace\\JNItest\\bin;
options [1] .optionString =-verbose;
options [2] .optionString =-verbose:jni;
memset(& vm_args,0,sizeof(vm_args));
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
status = JNI_CreateJavaVM(& jvm,(void **)& env,& vm_args);

if(status!= JNI_ERR)
{
cls = env-> FindClass(Sample2);
if(cls!= 0){
mid = env-> GetMethodID(cls,intMethod,(I)I);
if(mid!= 0){
square = env-> CallIntMethod(cls,mid,5); //这是所有崩溃的地方
printf(intMethod的结果:%d \\\
,square);
}
}
jvm-> DestroyJavaVM();
return 0;
}
else {
return -1;
}
}

程序实际上找到该方法, code> square = env-> CallIntMethod(cls,mid,5); 部分发生访问冲突。
如果我把它改变为一个静态方法,它运行正常,但我需要能够调用非静态方法...



任何想法 c> FindClass $ c>,您需要通过调用 NewObject 创建该类的实例。要这样做,首先需要获取构造函数...

  jmethodID constructor =(* env) - > GetMethodID env,cls,< init>,void(V)); 

然后创建一个对象

  jobject object =(* env) - > NewObject(env,cls,constructor); 

那么你可以调用实例函数

  square = env-> CallIntMethod(cls,mid,object,5); 


I'm trying to call a non static java method from c++.

Sample2.java:

public class Sample2 {
    public int intMethod(int n) {
        return n*n;
    }
}

JNITest.cpp:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <memory.h>
#include <jni_md.h>
#include <jni.h>
using namespace std;

#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    JavaVMOption options[3];
    static JNIEnv *env;
    JavaVM *jvm;
    JavaVMInitArgs vm_args;
    long status;
    jclass cls, stringClass;
    jmethodID mid;
    jstring jstr;
    jobjectArray args;

    jint square;

    options[0].optionString = "-Djava.class.path=D:\\Studie\\EXP\\Code\\Workspace\\JNItest\\bin"; //2APL\\build"; //Workspace\\JNItest\\bin";
    options[1].optionString = "-verbose";
    options[2].optionString = "-verbose:jni";
    memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 1;
    vm_args.options = options;
    status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

    if (status != JNI_ERR)
    {
        cls = env->FindClass("Sample2");
        if(cls !=0) {
            mid = env->GetMethodID(cls, "intMethod",  "(I)I");
            if(mid !=0) {
                square = env->CallIntMethod(cls, mid, 5); //this is where it all crashes
                printf("Result of intMethod: %d\n", square);
            }
        }
        jvm->DestroyJavaVM();
        return 0;
    }
    else {
        return -1;
    }
}

the program actually finds the method and gets to the square = env->CallIntMethod(cls, mid, 5); part where the access violation occurs. If I change it all to a static method it runs fine, but I need to be able to call non static methods as well...

Any thoughts on what I'm doing wrong here?

解决方案

After you've called FindClass, you need to create an instance of that class by calling NewObject. To do so, you first need to get the constructor ...

jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>", "void(V)");

then create an object

jobject object = (*env)->NewObject(env, cls, constructor);

then you can call the instance function

square = env->CallIntMethod(cls, mid, object, 5); 

这篇关于从C ++调用非静态java方法时的JNI访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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