System.load() problem

C

cppaddict

I have the following code to load a native .DLL in my java class:

static {
System.load("C:\\Java\\myNativeMethods.dll");
}

It had been working fine. Then I moved my project to a new folder and
gave it a new package name. However, I did not change or move my .DLL,
and the code that calls it (above) was untouched. However, I am now
getting an UnsatisifiedLinkError at runtime when my app hits the first
native method call. Why is this happening? How can I fix it?

Thanks,
cpp
 
T

Tor Iver Wilhelmsen

cppaddict said:
It had been working fine. Then I moved my project to a new folder and
gave it a new package name.

Then it no longer will find the native methods, because the package
name is a part of the native method name. E.g. a static method
getFoo() returning int in the class foo.test is called

JNIEXPORT jint JNICALL Java_foo_test_getFoo(JNIEnv *, jclass);

UnsatisfiedLink does *not* mean that the System.load() call failed,
but that it cannot find a native method in any loaded DLLs that match
the expected signature.

That said, you are also better off using System.loadLibrary(), ditch
the explicit path and instead use "-Djava.library.path=C:\Java" on the
command line.
 
G

Gordon Beaton

Then I moved my project to a new folder and gave it a new package
name. However, I did not change or move my .DLL, and the code that
calls it (above) was untouched. However, I am now getting an
UnsatisifiedLinkError at runtime when my app hits the first native
method call. Why is this happening? How can I fix it?

Since UnsatisifiedLinkError occurs at method invokation, it means the
JVM cannot find the required symbol in the DLL. (Had the error occured
at System.load() it would have meant that the JVM could not find the
DLL, or that loading failed for some other reason).

The native symbol names the JVM expects to find depend on the fully
qualified name of the class to which they belong. You changed the
package name, so essentially your native methods no longer belong to
the class.

Re-run javah to get a new list of symbol names, rename the symbols in
the C file, and recompile the DLL. Note that you need to specify the
_fully_qualified_ classname when you run javah.

If you call RegisterNatives() from JNI_OnLoad() you can name the
symbols anything you like, without regard to the package name or the
java-declared native method names. It lets you provide an explicit
mapping from the java-names of your native methods to the actual names
in the DLL source. RegisterNatives() still needs to know the fully
qualified classname, but since the information is in a single place
you don't need to re-run javah and you don't need to rename anything.

/gordon
 
P

Paul Ashby

use System.loadLibrary("myNativeMethods"); // no .dll

and make sure the location of myNativeMethods is in
your PATH system variable, or start the jvm with

java -Djava.library.path=<path for dll> com.....
 
C

cppaddict

use System.loadLibrary("myNativeMethods"); // no .dll
and make sure the location of myNativeMethods is in
your PATH system variable, or start the jvm with

Paul,

This is not the problem, just for future reference. The answer posted
by Gordon above is the reason for failure. System.load() with the
fully qualified path name works just fine, and the system PATH
variable doesn't come into play at all when you use that.

Thanks anyway for your response,
cpp
 
Joined
Sep 25, 2007
Messages
2
Reaction score
0
RegisterNatives JNI_OnLoad() generating stub error

cppaddict said:
>use System.loadLibrary("myNativeMethods"); // no .dll
>
>and make sure the location of myNativeMethods is in
>your PATH system variable, or start the jvm with


Paul,

This is not the problem, just for future reference. The answer posted
by Gordon above is the reason for failure. System.load() with the
fully qualified path name works just fine, and the system PATH
variable doesn't come into play at all when you use that.

Thanks anyway for your response,
cpp


Can u tell me how we sud proceed through register natives.
Because in my code dll generation changes the signature of the method and because of that unsatisfied link error is occured.
So i decided to work with register natives. but i dnt knw how to proceed.. do any stub required..???

Thanks in advance:grin:
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top