C++利用JNI访问Java,提示找不到class

zhblucky1 2014-12-15 03:38:05
c++利用jni访问java,代码结构如下:
[zhang@tc helloworld]$ pwd
/usr/home/zhang/jni/helloworld
[zhang@tc helloworld]$ ls
hello.cpp Makefile MyTest.java MyTest.java.bak README
[zhang@tc helloworld]$ echo $CLASSPATH
/usr/local/jdk/lib:/data1/hbase-0.96.2/hbase-client/src/main/java:/usr/home/zhang/javafun/class

MyTest.java:
//package MyTest;
class MyTest {
public MyTest(){
super();
}
public static int add(int a, int b) {
return a + b;
}
public boolean judge(boolean bool) {
return !bool;
}
public static void main(String[] args)
{
System.out.println("hello world");
}
}

hello.cpp:

#include<stdio.h>
#include<jni.h>
#include<sys/time.h>
#include<errno.h>
#include<string.h>

int main()
{
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
struct timeval start,stop;
struct timeval t1,t2,t3,t4;
float time_use;

options[0].optionString = "-Djava.class.path=.";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
gettimeofday(&start, NULL);
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (status != JNI_ERR)
{
gettimeofday(&t1, NULL);
//cls = env->FindClass("/usr/home/zhangbo6/javafun/class/MyTest");
//cls = env->FindClass("java/lang/String");
cls = env->FindClass("MyTest");
gettimeofday(&t2, NULL);
if (cls != 0)
{
printf("find class\n");
mid = env->GetStaticMethodID(cls, "add", "(II)I");
gettimeofday(&t3, NULL);
if (mid != 0)
{
square = env->CallStaticIntMethod(cls, mid, 5, 5);
gettimeofday(&t4, NULL);
printf("square:%d\n", square);
}
} else {
printf("can not find class\n");
}

jvm->DestroyJavaVM();
}
gettimeofday(&stop, NULL);
printf("used %d us, createJVM %d, findclass %d, getMethod %d, callMethod %d\n",
(stop.tv_sec-start.tv_sec)*1000000 + stop.tv_usec-start.tv_usec,
(t1.tv_sec-start.tv_sec)*1000000 + t1.tv_usec-start.tv_usec,
(t2.tv_sec-t1.tv_sec)*1000000 + t2.tv_usec-t1.tv_usec,
(t3.tv_sec-t2.tv_sec)*1000000 + t3.tv_usec-t2.tv_usec,
(t4.tv_sec-t3.tv_sec)*1000000 + t4.tv_usec-t3.tv_usec);

return 0;
}

Makefile:
default:hello tclass tjar
hello:hello.cpp
g++ -o hello hello.cpp -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -L${JRE_HOME}/lib/amd64/server -ljvm
tclass:MyTest.java
javac MyTest.java
tjar:tclass MyTest.class
jar -cvf MyTest.jar MyTest.class

clean:
rm -rf hello hello.o MyTest.class MyTest.jar

执行make&./hello:
[zhang@tc helloworld]$ make
g++ -o hello hello.cpp -I/usr/local/jdk/include -I/usr/local/jdk/include/linux -L/usr/local/jdk/jre/lib/amd64/server -ljvm
javac MyTest.java
jar -cvf MyTest.jar MyTest.class
added manifest
adding: MyTest.class(in = 567) (out= 375)(deflated 33%)
[zhang@tc helloworld]$ ./hello
find class
square:10
used 91964 us, createJVM 82602, findclass 7210, getMethod 90, callMethod 6

但是 我把MyTest.class 和 MyTest.jar mv到/usr/home/zhang/javafun/class/ 下,执行就无法找到MyTest.class:
[zhang@tc helloworld]$ mv MyTest.class /usr/home/zhang/javafun/class/
[zhang@tc helloworld]$ mv MyTest.jar /usr/home/zhang/javafun/class/
[zhang@tc helloworld]$ ls
hello hello.cpp Makefile MyTest.java MyTest.java.bak README
[zhang@tc helloworld]$ ls /usr/home/zhang/javafun/class/
MyTest.class MyTest.jar
[zhang@tc helloworld]$ echo $CLASSPATH
/usr/local/jdk/lib:/data1/hbase-0.96.2/hbase-client/src/main/java:/usr/home/zhang/javafun/class
[zhang@tc helloworld]$ ./hello
can not find class
used 83917 us, createJVM 79262, findclass 2671, getMethod -557925959, callMethod 366006104

还请熟悉的帮忙 给点意见,搞了快一天了,烦球滴,ps:对java不是很熟悉。

多谢!
...全文
3036 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
我在地球 2017-06-20
  • 打赏
  • 举报
回复
JNA更好。
santan 2017-06-04
  • 打赏
  • 举报
回复
options[0].optionString = "-Djava.class.path=."; 这个改了么
清泉流 2016-11-11
  • 打赏
  • 举报
回复
建议使用jna,他是jni封装的之后的,易学好用
happyzeng79 2016-09-18
  • 打赏
  • 举报
回复
我也碰到这个问题,感觉好难搞。

13,100

社区成员

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

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