Here I wrote my own tutorial for creating JNI "Hello, World!"
application.
I think it is more detailed than the SUN's one.
Try it,
Jacob Nikom
Tutorial
"In the "Lesson: Compiling and Running a Java Program with a Native
Method" the tutorial
author described six steps in writing JNI program:
1. Writing Java program with class declaring the native method.
2. Compile The Java class
3. Create a header file.
4. Write C language implementation of the native method
5. Compile the header and implementation files into shared library file
6. Run the Java program
I found that most of problems happen on the step 5 - creating shared
libraries.
To make things simpler I suggest to split step 5 into two steps:
5a. Compilation of; C language implementation of the native method
5b. Linking all stuff and creating the shared library.
I think it would be nice to be more specific and show the directory
content for
each step. For example, after the first step the directory listing will
be:
step 1: Writing HelloWorld.java file
my_directory>ls -l
HelloWorld.java
step 2: Java compilation
javac HelloWorld.java
my_directory>ls -l
HelloWorld.java
HelloWorld.class
step 3: Generating HelloWorld.h file (Don't use .java extension)
javah -jni HelloWorld
my_directory>ls -l
HelloWorld.java
HelloWorld.class
HelloWorld.h
step 4: Writing HelloWorldImp.c file using vi editor
my_directory>ls -l
HelloWorld.java
HelloWorld.class
HelloWorld.h
HelloWorldImp.c
step 5a: Compilation of C file
gcc -I//homes/nikom/work/Java/jdk1.2/include \
-I//homes/nikom/work/Java/jdk1.2/include/linux \
-c HelloWorldImp.c \
-o HelloWorldImp.o
Notice, you need to provide here the path to C include directory.
It is not obvious thing to do and you have to tell clearly
about it to the user.
My LD_LIBRARY_PATH is:
LD_LIBRARY_PATH=.:/local/lib:/usr/lib:/usr/lib/X11
my_directory>ls -l
HelloWorld.java
HelloWorld.class
HelloWorld.h
HelloWorldImp.c
HelloWorldImp.o
step 5b: creating the shared library
gcc -shared HelloWorldImp.o -o libhello.so
my_directory>ls -l
HelloWorld.java
HelloWorld.class
HelloWorld.h
HelloWorldImp.c
HelloWorldImp.o
libhello.so
step 6: Run the Program
java HelloWorld
Hello world!
Kevin McWhirter wrote:
>
> How does one compile/link the shared object for JNI modules.
>
> Here is an example the shows the problems I am having. I follow the steps
> in the Tutorial; namely:
> 1) Create Java code (HelloWorld.java)
> 2) Compile
> 3) Create header for stubs (HelloWorld.h)
> 4) Create C/C++ code (HelloWorld.C)
> 5) Compile/link with jni.h
> 6) Set LD_LIBRARY_PATH
> 7) Execute
>
> I end up with the following error message when I try to execute:
> Exception in thread "main" java.lang.UnsatisfiedLinkError:
> /home/klmcw/src/Java/JNI/libhwrld.so: /home/klmcw/src/Java/JNI/libhwrld.so:
> ELF file's phentsize not the expected size
> at java.lang.ClassLoader$NativeLibrary.load(Native Method)
> at java.lang.ClassLoader.loadLibrary0(Compiled Code)
> at java.lang.ClassLoader.loadLibrary(Compiled Code)
> at java.lang.Runtime.loadLibrary0(Compiled Code)
> at java.lang.System.loadLibrary(Compiled Code)
> at HelloWorld.<clinit>(HelloWorld.java:15)
>
> What is this ELF file phentsize error about? Any ideas? Thanks in advance.
>
> I have included the source below just in case.
> Kevin
>
> -+-+-+-+-+-+-+-+-+-+-+-CUT-+-+-+-+-+-+-+-+-+-+-+-
> HelloWorld.java
> class HelloWorld {
> String s = null;
>
> public HelloWorld() {
> s = HelloWorldAux();
> }
>
> public native String HelloWorldAux();
>
> public String toString() {
> return this.s;
> }
>
> static {
> System.loadLibrary("hwrld");
> }
>
> static public int main(String[] args) {
> try {
> HelloWorld hw = new HelloWorld();
>
> System.out.println(hw.toString());
> } finally {
> return 0;
> }
> }
> }
> -+-+-+-+-+-+-+-+-+-+-+-CUT-+-+-+-+-+-+-+-+-+-+-+-
> HelloWorld.C
>
> #include "HelloWorld.h"
>
> JNIEXPORT jstring JNICALL
> Java_HelloWorld_HelloWorldAux(JNIEnv *jenv, jobject jobj)
> {
> static char hw[] = "Hello, World!";
>
> return jenv->NewStringUTF(hw);
> }
> -+-+-+-+-+-+-+-+-+-+-+-CUT-+-+-+-+-+-+-+-+-+-+-+-
> Makefile
>
> INCLUDES=-I/opt/web/jdk1.2/include -I/opt/web/jdk1.2/include/linux
>
> CXXFLAGS=-c -fpic -O2 $(INCLUDES)
>
> all: libhwrld.so
>
> libhwrld.so: HelloWorld.C HelloWorld.h
> g++ $(CXXFLAGS) $< -o $@
>
> HelloWorld.h: HelloWorld.class
> javah -jni HelloWorld
>
> HelloWorld.class: HelloWorld.java
> javac -O HelloWorld.java
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]