native methods from java

B

BARBARA

I have a class containing native definition methods for calling an
external C service.
This class has no package, if I compile the class and use it with
jdk1.3.1 all is ok.
The problem is with jkd1.4.1 which does not accept java class without
package used in the import statement.
If I put this class (with native definition methods) in a package, I
have at run time the error java.lang.UnsatisfiedLinkError:
myNativeMethodName.

The LD_LIBRARY_PATH is set
The only problem is that I put the class in a package.

Can someone help me ???

Thank you for answer
Barbara
 
G

Gordon Beaton

I have a class containing native definition methods for calling an
external C service. [...]
If I put this class (with native definition methods) in a package, I
have at run time the error java.lang.UnsatisfiedLinkError:
myNativeMethodName.

When you invoke a native method, the JVM looks for a library method
that's partly generated from the name of the class, including the
package name (if any). That means that you can't just change the name
of the class or its package without renaming the methods in the native
code.

After recompiling the packaged class, use javah to generate a new set
of declarations. You need to specify the fully qualified classname
when you run javah, i.e. include the package name.

Now change the names of the methods in your C or C++ file according to
the declarations generated by javah, then recompile the shared library
or dll.

For a more flexible mechanism, look into using RegisterNatives()
instead. It lets you ignore javah and use whatever names you like,
then tell the JVM how to map the expected method names to the ones
actually provided by your library. Call it from JNI_OnLoad().

/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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top