1

I'm trying to get a working example of calling Java from C using JNI on ubuntu 64bit.
The code from: calling java function from c using jni I am compiling using:

gcc test.c -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux -c
gcc test.o -L/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server/ -ljvm -o jniTest

This doesnt generate any errors but if I try ldd jniTest I get the following:

linux-vdso.so.1 =>  (0x00007fffe55d5000)
libjvm.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f85f2928000)
/lib64/ld-linux-x86-64.so.2 (0x00007f85f2d0a000)

And if I try ./jniTest it gives this error:

test.o: In function `create_vm':
test.c:(.text+0x35): undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status

1 Answer 1

0

With the -L/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server/ you tell the linker where to find libjvm.so. At runtime you will need to do the same (http://linux.die.net/man/8/ld.so). And you'll also need the parent directory because other needed libs are in there. One way to do this is by specifying it on the command line: LD_LIBRARY_PATH=/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server:/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64 ./jniTest.

A way to get around the need to specify the path is to not link to libjvm.so at all but to find it at runtime and then use the functions in dlfcn.h (http://linux.die.net/man/3/dlopen, http://linux.die.net/man/3/dlsym) to use it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.