call nonstatic methods from java to cpp using JNI

瘦欲@ 提交于 2019-12-11 14:20:14

问题


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");

}

}

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

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!

回答1:


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

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




回答2:


void(V) is not a valid method descriptor.

Because there is no method called <init> with the descriptor void(V) (which there can't be, because it's invalid), GetMethodID returns 0. Then you try to create a new object using this invalid method ID.

The method descriptor for a method that takes no arguments and returns void (which a constructor is) is ()V.



来源:https://stackoverflow.com/questions/24007635/call-nonstatic-methods-from-java-to-cpp-using-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!