[英]JVM created via JNI in C doesn't work
I'm quite new to JNI and right now I'm using this simple C program to create a JVM and call the main() from my Java project: 我对JNI还是很陌生,现在我正在使用这个简单的C程序来创建JVM并从Java项目中调用main():
#include <stdio.h>
#include <jni.h>
JNIEnv* create_vm() {
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=CLASSPATH"; //This isn't the actual classhpath, but you get the idea
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
int ret = JNI_CreateJavaVM(&jvm, (void **)&env, &args);
if (ret<0){
printf("\nUnable to Launch JVM");
} else {
printf("\nJVM launched successfully");
}
return env;
}
void invoke_class(JNIEnv* env) {
jclass UncaughtExceptionClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;
UncaughtExceptionClass = (*env)->FindClass(env, "exceptioncatcher/ExceptionCatcher");
mainMethod = (*env)->GetStaticMethodID(env, UncaughtExceptionClass, "main", "([Ljava/lang/String;)V");
applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
applicationArg0 = (*env)->NewStringUTF(env, "From C");
(*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);
(*env)->CallStaticVoidMethod(env, UncaughtExceptionClass, mainMethod, applicationArgs);
}
int main(int argc, char **argv) {
JNIEnv* env = create_vm();
invoke_class( env );
}
This works well when running a HelloWorld type java code, but my code actually sets a Default Uncaught Exception Handler. 当运行HelloWorld类型的Java代码时,这很好用,但是我的代码实际上设置了一个Default Uncaught Exception Handler。 This handler will send the info collected for each uncaught exception to a thread that will process them and send them by email. 该处理程序会将针对每个未捕获的异常收集的信息发送到线程,该线程将对其进行处理并通过电子邮件发送。
Right now these are pretty much all the classes created and it, obviously, won't catch a lot of uncaught exceptions right now. 现在,这些几乎是所有创建的类,并且显然,现在不会捕获很多未捕获的异常。 But I use the main() to try it out: 但是我使用main()来尝试一下:
public static void main(String[] args){
Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler());
Integer i = null;
i++;
}
It works fine when I simply run it from netbeans, but the only thing that appears when using the C app is "JVM launched successfully". 当我只是从netbeans运行它时,它运行良好,但是使用C应用程序时出现的唯一一件事是“ JVM成功启动”。
Can anyone help on this one? 有人可以帮忙吗?
设法使其最终运行(从未关闭过问题),只需构建项目并将类路径更改为生成的.jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.