jni FindClass返回null

周靖峰 2012-08-14 08:36:55
小弟最近有个项目要用到jni来实现,在jni中需要引用自己写的类来返回某些值,但是在jni中使用FindClass,如果该类不是java自带的就会返回null,求解决方法

以下是测试代码:

test.A.java //自己定义的类,希望jni通过该类来返回数据

package test;

public class A
{
public int num;
}


test.B.java //测试类,看看jni是否能够成功返回A的对象

package test;

public class B
{
public static native A getA();

static
{
System.loadLibrary("A");
}

public static void main(String[] args)
{
System.out.println(getA().num);
}
}


test_B.h //jni的头文件

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class test_B */

#ifndef _Included_test_B
#define _Included_test_B
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: test_B
* Method: getA
* Signature: ()Ltest/A;
*/
JNIEXPORT jobject JNICALL Java_test_B_getA
(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif


test_B.c //jni的实现文件

#include "test_B.h"

/*
* Class: test_B
* Method: getA
* Signature: ()Ltest/A;
*/
JNIEXPORT jobject JNICALL Java_test_B_getA
(JNIEnv * env, jclass cl)
{
jclass class_A = (*env)->FindClass(env, "test.A"); //我已经调试过了,这句执行后class_A的结果为null
jfieldID fid_A_num = (*env)->GetFieldID(env, class_A, "num", "I");
jmethodID mid_A = (*env)->GetMethodID(env, class_A, "<init>", "()V");
jobject obj_A_a = (*env)->NewObject(env, class_A, mid_A);
(*env)->SetIntField(env, obj_A_a, fid_A_num, 100);
return obj_A_a;
}
...全文
1568 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
暮雨沉沦 2015-01-28
  • 打赏
  • 举报
回复
引用 10 楼 u012889234 的回复:
楼主,搞定了吗,我也遇到这样的问题。。。。 我看头文件中这样定义的。 jclass FindClass(const char *name) { return functions->FindClass(this, name); 这个name指的是绝对路径还是什么??为什么就找不到呢? 我们一起看吧!
name是包名加类名 比如;com/example/test test是一个类
(>^ω^<)喵上树 2014-08-25
  • 打赏
  • 举报
回复
楼主,搞定了吗,我也遇到这样的问题。。。。 我看头文件中这样定义的。 jclass FindClass(const char *name) { return functions->FindClass(this, name); 这个name指的是绝对路径还是什么??为什么就找不到呢? 我们一起看吧!
wwy_2008 2014-08-07
  • 打赏
  • 举报
回复
楼主搞定了吗?我也遇到了同样的问题。。。。。 在线等。。。。
c_huabo 2014-04-26
  • 打赏
  • 举报
回复
jclass class_A = (*env)->FindClass(env, "test/A");
matrix_xu 2013-04-26
  • 打赏
  • 举报
回复
test/A 不是 test.A
dracularking 2012-12-11
  • 打赏
  • 举报
回复
先试试4楼的
BadPattern 2012-12-11
  • 打赏
  • 举报
回复
FindClass jclass FindClass(JNIEnv *env, const char *name); This function loads a locally-defined class. It searches the directories and zip files specified by the CLASSPATH environment variable for the class with the specified name. LINKAGE: Index 6 in the JNIEnv interface function table. PARAMETERS: env: the JNI interface pointer. name: a fully-qualified class name (that is, a package name, delimited by “/”, followed by the class name). If the name begins with “[“ (the array signature character), it returns an array class. The string is encoded in modified UTF-8. 看上边,要以"/"分隔而不是"."
司机 2012-12-11
  • 打赏
  • 举报
回复
建议转成jstring之类的方式。class不靠谱
流星陨落 2012-12-10
  • 打赏
  • 举报
回复
引用 2 楼 hwndid 的回复:
最好不要再C中使用Java对象,你如果需要处理返回数据的话,可以定义字节数组作为一个方法的参数,这个Java和C通用,在Java中你自己再根据得到的字节数组把数据解析成对象
+1
我百了个度 2012-12-10
  • 打赏
  • 举报
回复
最好不要再C中使用Java对象,你如果需要处理返回数据的话,可以定义字节数组作为一个方法的参数,这个Java和C通用,在Java中你自己再根据得到的字节数组把数据解析成对象
l2484035 2012-12-10
  • 打赏
  • 举报
回复
楼主, 你解决没有呀, 我也有同样的问题呀

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧