1

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)

私が何か間違ったことをしているかどうか誰かに教えてもらえますか?

4

1 に答える 1