我必须制作一个在 C++ 中启动 Java 应用程序的程序。
C 中的第一个示例运行良好,它在 C 中完美地启动了 jar。但是当我在 C++ 中运行相同的进程时,我得到了一个异常。
这是C代码:
SetCurrentDirectoryA("..\\myJavaApp\\");
JavaVMInitArgs args;
int optionsLength = 4;
JavaVMOption options[optionsLength];
options[0].optionString = "-Djava.class.path=path\\myJar.jar";
options[1].optionString = "-Xbootclasspath/p:.\\lib\\BootJar.jar";
options[2].optionString = "-Djava.protocol.handler.pkgs=com.protocol";
options[3].optionString = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
int ret = JNI_CreateJavaVM(&jvm, (void **) &env, &args);
if (ret < 0) {
printf("\nUnable to Launch JVM %d ", ret);
} else {
printf("\nJVM launched successfully");
}
UncaughtExceptionClass = (*env)->FindClass(env, "mypackage/MyMainClass");
if (UncaughtExceptionClass == NULL) {
printf("\nClass not found.");
(*jvm)->DetachCurrentThread(jvm);
return;
}
mainMethod = (*env)->GetStaticMethodID(env, UncaughtExceptionClass, "main", "([Ljava/lang/String;)V");
if (mainMethod == NULL) {
printf("\nMethod not found.");
(*jvm)->DetachCurrentThread(jvm);
return;
}
(*env)->CallStaticVoidMethod(env, UncaughtExceptionClass, mainMethod, NULL);
(*jvm)->DetachCurrentThread(jvm);
在 C++ 中:
SetCurrentDirectoryA(cmdval.c_str());
JavaVMInitArgs args;
JavaVMOption options[5];
std::cout << " number of argument " << nbargs << std::endl;
args.version = JNI_VERSION_1_6;
args.nOptions = 5;
options[0].optionString = (char*) "-Djava.class.path=path\\myJar.jar";
options[1].optionString = (char*) "-Xbootclasspath/p:.\\lib\\BootJar.jar";
options[2].optionString = (char*) "-Djava.protocol.handler.pkgs=com.protocol";
options[3].optionString = (char*) "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000";
options[4].optionString = "-verbose:jni";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
int ret = JNI_CreateJavaVM(&jvm, (void **) &env, &args);
if (ret < 0)
{
// printf("\nUnable to Launch JVM %d ", ret);
}
else
{
printf("\nJVM launched successfully\n");
}
attachThread = (*jvm).AttachCurrentThread((void**) &env, NULL);
jclass mainClass =(*env).FindClass("mypackage/MyMainClass");
这是我在 C++ 中遇到的异常:
Exception in thread "main" java.lang.NoClassDefFoundError: mypackage/MyMainClass
Caused by: java.lang.ClassNotFoundException: mypackage.MyMainClass
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
有人可以告诉我我做错了什么吗?