【问题标题】:call nonstatic methods from java to cpp using JNI使用 JNI 将非静态方法从 java 调用到 c++
【发布时间】:2014-06-05 13:14:48
【问题描述】:

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

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++ 代码在这里:

#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 运行时环境检测到致命错误:

 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!

【问题讨论】:

    标签: java java-native-interface jnienv


    【解决方案1】:

    除了immibis'的接听,我也想打个电话

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

    没有返回任何东西,因为你的班级被称为

    public class hellojava
    

    所以您的应用程序可能在 GetMethodID()NewObject() 中出现段错误

    【讨论】:

      【解决方案2】:

      void(V) 不是有效的方法描述符。

      因为不存在名为&lt;init&gt; 且描述符为void(V) 的方法(不可能存在,因为它无效),GetMethodID 返回 0。然后您尝试使用这个无效的方法 ID 创建一个新对象.

      不带参数并返回void(即构造函数)的方法的方法描述符是()V

      【讨论】:

      • javap -classpath /home/../nonstaticJavaMethods/ -s -public hellojava | egrep -A 2 "&lt;init&gt;"
      • @TomBlodget 没有名为&lt;init&gt;的方法使用描述符void(V)
      • 哦,对了,对于构造函数来说并不那么简单。无论如何,按类名查询,但在 JNI 中使用“”。这是一个更好的例子:javap -s -public java.lang.String | egrep -A 2 "String\("
      • @TomBlodget 构造函数与其他方法相同,只是名称始终为&lt;init&gt;,返回类型始终为V(对应于Java 中的void)。 void(V) 不是任何方法的有效描述符 - 对于不带参数并返回 void 的方法,它应该是 ()V
      猜你喜欢
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多