Hi-
I've successfully execute the JNI examples, and have a nice hello world
program that uses JNI. However, the test examples use the default
package. When I try to move my code to another package, I get
unsatisfied link errors.
Here's the sitch:
I have a package, test.jni that contains JNITest.java. There is a
native method in it called helloWorld() that I attempt to call. To
build the shared object, I did the following:
javah -jni test/jni/JNITest
in the directory I ran the command from I get:
test_0002fjni_0002fJNITest.h
My c file, the originally named jnitest.c is in the same directory.
I compile it all together via:
gcc -shared -olibblah.so -static -lc jnitest.c
-I/usr/java/j2sdk1.4.2_05/include/ -I
/usr/java/j2sdk1.4.2_05/include/linux/
The result is I get libblah.so
libblah.so contains helloWorld:
[root@localhost JNI Test]# nm libblah.so |head -3
00000700 T Java_test_0002fjni_0002fJNITest_helloWorld
000017e8 A _DYNAMIC
000017bc A _GLOBAL_OFFSET_TABLE_
I compile my java file with:
[root@localhost JNI Test]# javac test/jni/JNITest.java
I get no errors or warnings.
I then attempt to run it:
[root@localhost JNI Test]# java test/jni/JNITest
Exception in thread "main" java.lang.UnsatisfiedLinkError: helloWorld
at test.jni.JNITest.helloWorld(Native Method)
at test.jni.JNITest.main(JNITest.java:15)
This works just fine if I have things in the default package. Moving
it to another package, regardless of how I build this thing fail with
the above.
My JNITest.java is below ( and below that, jnitest.c ). Any ideas?
All the examples seem to be in the default package, so I figure I am
probably doing something stupid.
package test.jni;
public class JNITest
{
static {
System.loadLibrary( "blah" );
}
public native void helloWorld();
public static void main ( String[] args )
{
JNITest test = new JNITest();
test.helloWorld();
}
}
// end
jnitest.c:
#include <stdio.h>
#include "test_0002fjni_0002fJNITest.h"
JNIEXPORT void JNICALL Java_test_0002fjni_0002fJNITest_helloWorld
(JNIEnv * env, jobject obj)
{
printf ( "Hello world\n" );
}
Gordon Beaton - 02 Mar 2006 07:08 GMT
> I have a package, test.jni that contains JNITest.java. There is a
> native method in it called helloWorld() that I attempt to call. To
[quoted text clipped - 4 lines]
> in the directory I ran the command from I get:
> test_0002fjni_0002fJNITest.h
It looks like you spelled the classname incorrectly. Try this instead:
javah test.jni.JNITest
(-jni is default IIRC)
Then make sure your native code uses the symbolnames thus generated.
[...]
> I compile my java file with:
> [root@localhost JNI Test]# javac test/jni/JNITest.java
You must do this *before* running javah.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
chris.madden@gmail.com - 02 Mar 2006 12:54 GMT
Thank you very much! That worked perfectly