jni on windows (with cygwin) gives UnsatisfiedLinkError

J

johnmmcparland

Hi all,

I am learning to use JNI by following the walkthrough contained here;

http://java.sun.com/docs/books/jni/html/jniTOC.html

The only difference is that I call the files Hello instead of
HelloWorld.

I have managed to compile both Hello.java and compile Hello.c into the
library Hello.dll using this command;

gcc -mno-cygwin -I"${JAVA_PATH}/include" -I"${JAVA_PATH}/include/
win32" -W1,--add-stdcall-alias -shared -o Hello.dll Hello.c

where JAVA_PATH is the path to the java directory as cygwin sees it.

However I get an UnsatisfiedLinkError when I try to run the program;

$> java Hello
Exception in thread "main" java.lang.UnsatisfiedLinkError:
Hello.print()V
at Hello.print(Native Method)
at Hello.main(Hello.java:25)

I am suspicious of the "V" at the end of the Hello.print() line of the
exception message.

Does anyone have an idea why this is happening?

John
 
T

Thomas Fritsch

johnmmcparland said:
I am learning to use JNI by following the walkthrough contained here;

http://java.sun.com/docs/books/jni/html/jniTOC.html

The only difference is that I call the files Hello instead of
HelloWorld.

I have managed to compile both Hello.java and compile Hello.c into the
library Hello.dll using this command;

gcc -mno-cygwin -I"${JAVA_PATH}/include" -I"${JAVA_PATH}/include/
win32" -W1,--add-stdcall-alias -shared -o Hello.dll Hello.c

where JAVA_PATH is the path to the java directory as cygwin sees it.

However I get an UnsatisfiedLinkError when I try to run the program;

$> java Hello
Exception in thread "main" java.lang.UnsatisfiedLinkError:
Hello.print()V
at Hello.print(Native Method)
at Hello.main(Hello.java:25)

I am suspicious of the "V" at the end of the Hello.print() line of the
exception message.
The "V" should be OK. It is probably the way how the "void" return-type is
displayed.
Does anyone have an idea why this is happening?
The C-function Java_Hello_print is missing in your DLL (or it is not
exported with "JNIEXPORT").

I'm familiar with Microsoft's compiler/linker, but not with Cygwin's.
That said, I would check these:
(*) Did you run javah on your "Hello" class (after your renaming
from "HelloWorld" to "Hello")?
(*) Do you see
JNIEXPORT void JNICALL Java_Hello_print(JNIEnv *, jobject);
in your "Hello.h" file?
(*) Do you include "Hello.h" from your "Hello.c" file?
(*) Do you have the C function
Java_Hello_print(JNIEnv *env, jobject obj) {...}
in your "Hello.c" file?
 
T

Thomas Fritsch

Thomas said:
johnmmcparland wrote: [...]
gcc -mno-cygwin -I"${JAVA_PATH}/include" -I"${JAVA_PATH}/include/
win32" -W1,--add-stdcall-alias -shared -o Hello.dll Hello.c [...]
$> java Hello
Exception in thread "main" java.lang.UnsatisfiedLinkError:
Hello.print()V
at Hello.print(Native Method)
at Hello.main(Hello.java:25)
[...]
I'm familiar with Microsoft's compiler/linker, but not with Cygwin's.
That said, I would check these:
(*) Did you run javah on your "Hello" class (after your renaming
from "HelloWorld" to "Hello")?
(*) Do you see
JNIEXPORT void JNICALL Java_Hello_print(JNIEnv *, jobject);
in your "Hello.h" file?
(*) Do you include "Hello.h" from your "Hello.c" file?
(*) Do you have the C function
Java_Hello_print(JNIEnv *env, jobject obj) {...}
in your "Hello.c" file?
One further check:
(*) Did you load your "Hello.dll" from your Java side? Is there
static { System.loadLibrary("Hello"); }
in your "Hello.java"?
 
J

johnmmcparland

Thomas said:
johnmmcparland wrote: [...]
gcc -mno-cygwin -I"${JAVA_PATH}/include" -I"${JAVA_PATH}/include/
win32" -W1,--add-stdcall-alias -shared -o Hello.dll Hello.c [...]
$> java Hello
Exception in thread "main" java.lang.UnsatisfiedLinkError:
Hello.print()V
at Hello.print(Native Method)
at Hello.main(Hello.java:25)
[...]
I'm familiar with Microsoft's compiler/linker, but not with Cygwin's.
That said, I would check these:
(*) Did you run javah on your "Hello" class (after your renaming
from "HelloWorld" to "Hello")?
(*) Do you see
JNIEXPORT void JNICALL Java_Hello_print(JNIEnv *, jobject);
in your "Hello.h" file?
(*) Do you include "Hello.h" from your "Hello.c" file?
(*) Do you have the C function
Java_Hello_print(JNIEnv *env, jobject obj) {...}
in your "Hello.c" file?

One further check:
(*) Did you load your "Hello.dll" from your Java side? Is there
static { System.loadLibrary("Hello"); }
in your "Hello.java"?

Hi Thomas,

thanks for replying. I can confirm that;

1. ran javah on the Hello class to create Hello.h

2. I did see JNIEXPORT void JNICALL Java_Hello_print(JNIEnv *,
jobject); in the Hello.h file (see below);

// extract from Hello.h

#include <jni.h>

extern "C" {
JNIEXPORT void JNICALL Java_Hello_print

(JNIEnv *, jobject);
}

3. Included Hello.h in the Hello.c file (see below);

// extract from Hello.c

#include <jni.h>
#include <stdio.h>
#include "Hello.h"

JNIEXPORT void JNICALL
Java_Hello_print(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}

4. I have the Java_Hello_print function in Hello.c (see above).

5. I do load the Hello.dll from the Java side (see below);

// extract of Hello.java

static
{
System.loadLibrary("Hello");
}

6. Despite all of this I still get the error that I originally posted.

Any ideas?

John
 
G

Gordon Beaton

4. I have the Java_Hello_print function in Hello.c (see above).

It is apparent from your original post that System.loadLibrary() is
not the culprit. That means that the correct symbol was not found in
the DLL.

Check that the *DLL* contains a symbol with exactly that name, i.e.
that the compiler has not modified the symbol name in any way with
suffixes or similar.

Note the link option should start with -Wl ("ell"), not -W1 (one) as
in your earlier post. I would have expected an error from that.

Is there a package declaration in Hello.java?

/gordon

--
 
J

johnmmcparland

It is apparent from your original post that System.loadLibrary() is
not the culprit. That means that the correct symbol was not found in
the DLL.

Check that the *DLL* contains a symbol with exactly that name, i.e.
that the compiler has not modified the symbol name in any way with
suffixes or similar.

Note the link option should start with -Wl ("ell"), not -W1 (one) as
in your earlier post. I would have expected an error from that.

Is there a package declaration in Hello.java?

/gordon

--

Gordon,

Thanks for your reply.

In your post you said;
Note the link option should start with -Wl ("ell"), not -W1 (one) as
in your earlier post. I would have expected an error from that.

Changing 1 (one) to l (ell) worked fixed the problem I was having.
You'd think that we could type our posts in courier new font instead
of arial, that might help matters.

Out of interest, there is no package declaration in Hello.java. When
I put it into a package and modified my compilation / build / linking
as necessary everything worked fine.

Thanks,

John
 
G

Gordon Beaton

Changing 1 (one) to l (ell) worked fixed the problem I was having.
You'd think that we could type our posts in courier new font instead
of arial, that might help matters.

Glad that fixed it for you. I had no problem seeing the difference
between the characters. I read news with slrn in an xterm with a
default fixed font on Linux.

At any rate this just shows the value of cutting and pasting exact
commands and messages directly into your post as you did here.

/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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top