How to compile dynamic library for a JNI application on linux?
Native library can be loaded by loadLibrary with a valid name. By example, libXXXX.so for linux family, your hellolib.so should rename to libhello.so. By the way, I develop java with jni, I will separate the implementation and native interface (.c or .cpp).
static {
System.loadLibrary("hello"); // will load libhello.so
}
The implementation header(HelloImpl.h):
#ifndef _HELLO_IMPL_H
#define _HELLO_IMPL_H
#ifdef __cplusplus
extern "C" {
#endif
void sayHello ();
#ifdef __cplusplus
}
#endif
#endif
HelloImpl.cpp:
#include "HelloImpl.h"
#include <iostream>
using namespace std;
void sayHello () {
cout << "Hello World!" << endl;
return;
}
Hello.c (I prefer to compile jni in c):
#include <jni.h>
#include "Hello.h"
#include "HelloImpl.h"
JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {
sayHello();
return;
}
Finally, we can compile them in some steps:
- compile obj (generate HelloImpl.o)
g++ -c -I"/opt/java/include" -I"/opt/java/include/linux" HelloImpl.cpp
- compile jni with .o
g++ -I"/opt/java/include" -I"/opt/java/include/linux" -o libhello.so -shared -Wl,-soname,hello.so Hello.c HelloImpl.o -static -lc
in step 2, we use g++ to compile it. This is very important. yor can see How to mix C and C++
After compilation, you can check the function naming with nm:
$ nm libhello.so |grep say
00000708 T Java_Hello_sayHello
00000784 t _GLOBAL__I_sayHello
00000718 T sayHello
There is a Java_Hello_sayHello marked T. It should extactly equal to your native method name. If everything is ok. you can run it:
$ java -Djava.library.path=. Hello
Hello World!
Finally my code works. This is hello.java
public class hello {
public native void sayHello(int length) ;
public static void main (String args[]) {
String str = "I am a good boy" ;
hello h = new hello () ;
h.sayHello (str.length() ) ;
}
static {
System.loadLibrary ( "hello" ) ;
}
}
You should compile it as :
$ javac hello.java
To create .h file you should run this command:
$ javah -jni hello
This is hello.h
:
JNIEXPORT void JNICALL Java_hello_sayHello
(JNIEnv *, jobject, jint);
Here is hello.c
:
#include<stdio.h>
#include<jni.h>
#include "hello.h"
JNIEXPORT void JNICALL Java_hello_sayHello
(JNIEnv *env, jobject object, jint len) {
printf ( "\nLength is %d", len ); }
To compile this and to create a shared library we have to run this command :
$ gcc -I/usr/lib/jvm/java-6-openjdk/include -o libhello.so -shared hello.c
Then finally run this one :
$ java -Djava.library.path=. hello
This complains about the C++ symbols not being available. I seem to remember, when I use to do JNI stuff all of the time that there were problems linking in C++ libraries and we always stuck to plain old C
If you change your code so that it's standard C (and rename the file):
#include <jni.h>
#include "Hello.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {
printf("Hello World");
return;
}
And compile it
gcc -I/usr/lib/jvm/java-6-openjdk/include -o libhellolib.so -shared Hello.c
It works
java -Djava.library.path=`pwd` Hello
Hello World