Test.java
import java.awt.Frame;
public class Test {
public static void main(String[] args) {
try {
System.out.println("test main");
Frame t = new Frame();
System.out.println("test main2");
} catch (Exception e) {
System.out.println("Catch");
}
finally {
System.out.println("Finally");
}
}
}
test.cc
JNIEnv *_VMEnvironment;
JavaVM *_VM;
jclass _JavaClass;
void init()
{
const int tCount = 1;
JavaVMInitArgs tArgs;
JavaVMOption tOptions[tCount];
tArgs.version = JNI_VERSION_1_6;
tArgs.nOptions = tCount;
std::string tPath = "-Djava.library.path=/usr/lib";
tOptions[0].optionString = const_cast<char*>(tPath.c_str());
tArgs.options = tOptions;
tArgs.ignoreUnrecognized = JNI_FALSE;
JNI_CreateJavaVM(&_VM, (void**)&_VMEnvironment, &tArgs);
}
void start()
{
jmethodID tMainMethod;
_JavaClass = _VMEnvironment->FindClass("Test");
tMainMethod = _VMEnvironment->GetStaticMethodID(
_JavaClass, "main", "([Ljava/lang/String;)V");
_VMEnvironment->CallStaticVoidMethod(_JavaClass, tMainMethod, nullptr);
std::cout << "Done" << std::endl;
}
int main()
{
init();
start();
return 0;
}
如果我链接到libverify, libjava, libjvm
,我会得到以下输出:
test main
test main2
Finally
Done
但是,如果我也链接到libjawt, libmawt, libawt
,我会得到以下输出:
test main
Finally
Done
我很困惑为什么第二个println(貌似)没有被执行。我尝试创建其他对象类型而不是框架,并没有看到问题发生。同样,我尝试了各种awt对象类型,并始终看到相同的结果。作为参考,我使用java 1.7.0_51运行64位数。