1
votes

I followed this tutorial http://www.ibm.com/developerworks/java/tutorials/j-jni/section3.html

and I compiled the cpp program using

g++ -o libSample2 -fPIC -shared -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux/ Sample2.cpp

I tried to run the ./libSample2 but I got "Segmentation fault (core dumped)"

Any idea?

1
Its a common error when you try to access area of memory which is outside your program's allocated address. Use valgrind to debugDeepankar Bajpeyi
Most probably you had missed something, so you can try some other tutorial as i mostly get chance to play with JNI and it, my experience say this verify you followed each and everything correctly in case of some missing step you can try some other tutorial i.e. codeproject.com/Articles/22881/…Saqlain

1 Answers

2
votes

Thank you guys, I figured out the problem. I ran this command

g++ -I /usr/lib/jvm/java-7-openjdk-amd64/include -I /usr/lib/jvm/java-7-openjdk-amd64/include/linux/ -L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -l jvm Sample2.cpp

and it didn't work. Then I ran it as below

g++ -I /usr/lib/jvm/java-7-openjdk-amd64/include -I /usr/lib/jvm/java-7-openjdk-amd64/include/linux/ -L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server Sample2.cpp -l jvm -Wl,-rpath,/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -o sample2

and believe it or not, it was the Ubuntu version that caused the problem. For some reason, it won't work for the above command.