P
protoplasm
I've followed Sun's "The Java Native Interface" by Sheng Liang on
creating a JNI test/example. Unfortunately it isn't working for me.
This leads me to believe that either I'm doing something stupid or
things have changed in later versions of Java.
Environment information:
$ java -version
java version "1.4.2_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_16-b05)
Java HotSpot(TM) Client VM (build 1.4.2_16-b05, mixed mode)
$ cat /etc/SuSE-release
SUSE LINUX Enterprise Server 9 (x86_64)
VERSION = 9
PATCHLEVEL = 3
$ gcc --version
gcc (GCC) 3.3.3 (SuSE Linux)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
$ uname -a
Linux cressida 2.6.5-7.287.3-default #1 Tue Oct 2 07:31:36 UTC 2007
x86_64 x86_64 x86_64 GNU/Linux
The below source code is taken directly from the book. Here is my
HelloWorld.java file:
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
Here is the HelloWorld.c file:
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}
Steps to build:
$ javac HelloWorld.java
$ javah -jni HelloWorld
$ gcc -O2 -fPIC -pthread -W -Wall -Wno-unused -Wno-parentheses -pipe -
D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_REENTRANT -D_LITTLE_ENDIAN -
D_LP64=1 -c HelloWorld.c
$ gcc -z defs -Wl,-O1 -Wl,-soname=libHelloWorld.so -static-libgcc -
shared -mimpure-text -o libHelloWorld.so -lc
Run the Java app:
$ java -Djava.library.path=. HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/
sprotsman/workspace/HelloWorldJ2/libHelloWorld.so: /home/sprotsman/
workspace/HelloWorldJ2/libHelloWorld.so: cannot open shared object
file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1586)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1511)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:834)
at HelloWorld.<clinit>(HelloWorld.java:7)
Any help on what I'm doing wrong would be much appreciated.
--Shawn
creating a JNI test/example. Unfortunately it isn't working for me.
This leads me to believe that either I'm doing something stupid or
things have changed in later versions of Java.
Environment information:
$ java -version
java version "1.4.2_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_16-b05)
Java HotSpot(TM) Client VM (build 1.4.2_16-b05, mixed mode)
$ cat /etc/SuSE-release
SUSE LINUX Enterprise Server 9 (x86_64)
VERSION = 9
PATCHLEVEL = 3
$ gcc --version
gcc (GCC) 3.3.3 (SuSE Linux)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
$ uname -a
Linux cressida 2.6.5-7.287.3-default #1 Tue Oct 2 07:31:36 UTC 2007
x86_64 x86_64 x86_64 GNU/Linux
The below source code is taken directly from the book. Here is my
HelloWorld.java file:
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
Here is the HelloWorld.c file:
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}
Steps to build:
$ javac HelloWorld.java
$ javah -jni HelloWorld
$ gcc -O2 -fPIC -pthread -W -Wall -Wno-unused -Wno-parentheses -pipe -
D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_REENTRANT -D_LITTLE_ENDIAN -
D_LP64=1 -c HelloWorld.c
$ gcc -z defs -Wl,-O1 -Wl,-soname=libHelloWorld.so -static-libgcc -
shared -mimpure-text -o libHelloWorld.so -lc
Run the Java app:
$ java -Djava.library.path=. HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/
sprotsman/workspace/HelloWorldJ2/libHelloWorld.so: /home/sprotsman/
workspace/HelloWorldJ2/libHelloWorld.so: cannot open shared object
file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1586)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1511)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:834)
at HelloWorld.<clinit>(HelloWorld.java:7)
Any help on what I'm doing wrong would be much appreciated.
--Shawn