我试着从c++代码中调用一些java类。我试着这样做:
java
package com.make.sign;
public class MakeSign {
public static void main(String[] args) {
System.out.println("Hello, world!");
if (1 <= args.length) {
System.out.println(args[0]);
}
}
}
从c++:
const int kNumOptions = 3;
JavaVMOption options[kNumOptions] = {
{ const_cast<char*>("-Xmx128m"), NULL },
{ const_cast<char*>("-verbose:gc"), NULL },
{ const_cast<char*>("-Djava.class.path=/home/my/JavaCTest"), NULL }
};
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = sizeof(options) / sizeof(JavaVMOption);
assert(vm_args.nOptions == kNumOptions);
JNIEnv* env = NULL;
JavaVM* jvm = NULL;
int res = JNI_CreateJavaVM(&jvm, reinterpret_cast<void**>(&env), &vm_args);
if (res != JNI_OK) {
std::cerr << "FAILED: JNI_CreateJavaVM " << res << std::endl;
return ;
}
const char* kClassName = "com/make/sign/MakeSign";
jclass cls = env->FindClass(kClassName);
if (cls == NULL) {
std::cerr << "FAILED: FindClass" << std::endl;
return ;
}
const char* kMethodName = "main";
jmethodID mid =
env->GetStaticMethodID(cls, kMethodName, "([Ljava/lang/String;)V");
if (mid == NULL) {
std::cerr << "FAILED: GetStaticMethodID" << std::endl;
return ;
}
const jsize kNumArgs = 1;
jclass string_cls = env->FindClass("java/lang/String");
jobject initial_element = NULL;
jobjectArray method_args = env->NewObjectArray(kNumArgs, string_cls, initial_element);
jstring method_args_0 = env->NewStringUTF("Hello, Java!");
env->SetObjectArrayElement(method_args, 0, method_args_0);
env->CallStaticVoidMethod(cls, mid, method_args);
jvm->DestroyJavaVM();
我为makesign.java制作了javac,将makesign.class放到/home/my/javactest/。运行build c++程序并获取:failed:findclass
请帮助,为什么找不到类以及如何修复?谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!