I am writing a java native library in c++, and using exception handling within native lib itself, but the library crashes as soon as I throw exception. Here is my simple test program, when I call it from Java test, it just crashes as soon as exception is thrown. The catch block is not working. Any ideas what i am missing. Thanks.
#include "Test.h"
#include <iostream>
JNIEXPORT void JNICALL Java_Test_helloWorld(JNIEnv *, jobject)
{
std::cout<<"Hello World";
try {
throw 1;
}
catch(int )
{
std::cout<<" catch int block"<<std::endl;
}
catch(...)
{
std::cout<<" catch block"<<std::endl;
}
}
Compile and Link:
g++ -m64 -fPIC -fexceptions -c test.cpp
g++ -shared -m64 -Wl,-soname,libtest.so -Wl,-shared-libgcc test.o -o libtest.so
$ java -d64 -Djava.library.path=/home/vkumar/projects/test -cp $CLASSPATH Test
terminate called after throwing an instance of 'int'
terminate called recursively
Hello World^CAbort (core dumped)
I tried your examplea and everything went fine. My environment is Ubuntu 12.04 (64bit) with Oracle JDK 1.7.
So, my guess is your environment is the culprit. Since you use option -m64
, it could be a mismatch between 32 bit system and 64 bit libtest.so.
Please verify your system, JDK, gcc, etc. fits all together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With