Package scope turbulence with JNI

C

chris.madden

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" );
}
 
G

Gordon Beaton

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top