使用JNI从Java到cpp调用非静态方法 [英] call nonstatic methods from java to cpp using JNI

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

问题描述

我正在尝试使用JNI从Java到C ++调用非静态方法 我的Java代码在这里:

I am trying to call non static method from java to C++ using JNI My Java Code is here:

public class hellojava
{
  public static void main(String args[]) 
  {
     System.out.println("Hello World!");
     System.out.println("This is the main function from the HelloWorld java class.");
  }

public void message()
{
    System.out.println("call from object");

}

}

我的C ++代码在这里:

And my C++ code is here:

#include <stdio.h>
#include <jni.h>


JNIEnv* create_vm(JavaVM ** jvm) {

JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;

options.optionString = "-Djava.class.path=/home/../nonstaticJavaMethods/";     
//Path to the java source code

vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;


int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0)
    printf("\nUnable to Launch JVM\n");     
return env;
}

int main(int argc, char* argv[])
{
JNIEnv *env;
JavaVM * jvm;
env = create_vm(&jvm);
if (env == NULL)
    return 1;   


//jclass clsH=NULL;
jmethodID midMain = NULL;
jstring square;
jclass clsH = env->FindClass("helloWorld");
jmethodID constructor = env->GetMethodID(clsH, "<init>", "void(V)");
jobject object = env->NewObject(clsH, constructor);



//Obtaining Method IDs
if (clsH != NULL)
{          midMain = env->GetMethodID(clsH, "message", "void(V)");
           env->CallVoidMethod(clsH, midMain, object,NULL); 
}
else
{
    printf("\nUnable to find the requested class\n");       
}




//Release resources.
int n = jvm->DestroyJavaVM();
return 0;

}

My code compiles but it is giving me runtime error. Following is the error

Java运行时环境检测到致命错误:

A fatal error has been detected by the Java Runtime Environment:

 SIGSEGV (0xb) at pc=0x00007fdf3f5126bb, pid=11302, tid=140596827092800

JRE version: OpenJDK Runtime Environment (7.0_55-b14) (build 1.7.0_55-b14)
Java VM: OpenJDK 64-Bit Server VM (24.51-b03 mixed mode linux-amd64 compressed oops)
Problematic frame:
V  [libjvm.so+0x5c46bb]  alloc_object(_jclass*, Thread*)+0x1b

 Failed to write core dump. Core dumps have been disabled. To enable core dumping,    
 try    
 "ulimit -c unlimited" before starting Java again

 An error report file with more information is saved    as  

 /home/../nonstaticJavaMethods/hs_err_pid11302.log      
 Aborted!

推荐答案

除了 immibis'的答案,我还认为通话

Additionally to immibis' answer, i also think the call

jclass clsH = env->FindClass("helloWorld");

什么也不返回,因为您的班级被称为

isn't returning anything, as your class is called

public class hellojava

因此您的应用程序可能在GetMethodID()NewObject()

So your application is probably seg-faulting in GetMethodID() or NewObject()

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

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