Why I can't call my native method from my package?

it is fine if my java code didn't complied into a package.

But if I complied my java code as a package, and call its native method outside. It give me "UnsatisfiedLinkError". And can not find my method. It is indeed the method is inside.

Thank a lot any helps. Email me on d.zhu@uea.ac.uk

[316 byte] By [daan75] at [2007-9-26 10:54:35]
# 1

What do you mean by compiling "into a package"? Your native method should be available as shared library so*.lib on Unix-derivates or *.dll on Wins. Make sure you use the correct method signatures as provided by javah and make the library available when you run your app.

Check out:

http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/_library.html

thpreusser at 2007-7-1 23:38:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

I have found that javah does not generate the correct JNI function names when the native function is in a class that is within a Java package. The net result is an Unsatisfied link error.

The JNI function name must be included in the package name. The naming is a bit complicated and that's why javah should be used, except in this case it does not work. I can't quite remember exactly how it works, something like adding in _package_name_1 into the JNI name. The SWIG tool (http://www.swig.org) does generate the correct names when using packages. It is a tool which takes C or C++ header files and generates the JNI and Java classes for you so that you can call C/C++ code from Java. Once you have installed SWIG and run 'make check', have a look in the directory Examples/test-suite/java for the JNI naming for packages. I'll try remember to post the exact naming if you don't want to install SWIG.

Guest at 2007-7-1 23:38:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
You guys must be doing something different or odd. From java.h I get function headers that include all the package preamble.
bschauwe at 2007-7-1 23:38:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
1. use the command line to genorate .h2. use javah -jni com.mypackage.mynativeClass to genorate .h , carefully, you must outside your package to run it. you will get a com_mypackage_mynativeClass.h
anneff at 2007-7-1 23:38:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

If I run this through javah ( version 1.3.1):

// file test.java

package const_const;

public class test {

public final static native void foo(long jarg0);

}

it produces this header file test.h:

...

JNIEXPORT void JNICALL Java_test_foo (JNIEnv *, jclass, jlong);

...

It should be:

JNIEXPORT void JNICALL Java_const_1const_test_foo(JNIEnv *, jclass, jlong);

I get this to link and run.

I ran javah in the package directory, ie in directory test. I've just tried running it from the directory above it and get

// In file const_0005fconst_0005ctest.h:

JNIEXPORT void JNICALL Java_const_1const_0005ctest_foo (JNIEnv *, jclass, jlong);

This doesn't look right to me, I'm not sure if this will link or not.

Guest at 2007-7-1 23:38:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

Thanks a lot for everyone's helps and care of my question.

I have solved the problem. The main

problem for it that "javah" can not generate a correct header file for

java package. Therefore, i have to manually modify it and insert the package path for the header file.

For exampel, for the normal header declare like that

******************************************************

JNIEXPORT jbyteArray JNICALL

Java_CLASSNAME_functionname(JNIEnv *env, jobject obj)

******************************************************

but for the package header declare like that

*****************************************************

JNIEXPORT jbyteArray JNICALL

Java_PackagePath_CLASSNAME_functionname(JNIEnv *env, jobject obj)

*****************************************************************

We have to do some change for the header file.

Thank a again for everone's kindly helps.

daan75 at 2007-7-1 23:38:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...