Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Basic JNI error - UnsatisfiedLinkError displayhelloworld

106 views
Skip to first unread message

andy

unread,
Apr 22, 2002, 12:46:56 AM4/22/02
to
Greetings, I followed the Sun tutorial verbatim
(http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/step6.html)
and get the UnsatisfiedLinkError displayhelloworld error and I read
other postings about putting the java file in a package, I even added
that to my .java file and nothing worked (package foo.bar.test.hw;).
the hello.dll is in the correct path, so I am confused.. any help
would be appreciated !!

Exception in thread "main" java.lang.UnsatisfiedLinkError:
displayHelloWorld
at HelloWorld.displayHelloWorld(Native Method)
at HelloWorld.main(helloworld.java:9)

.JAVA:
class HelloWorld {
public native void displayHelloWorld();

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

public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}

HEADER:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

HelloWorldImp.c.
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>

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

Gordon Beaton

unread,
Apr 22, 2002, 2:24:17 AM4/22/02
to
On 21 Apr 2002 21:46:56 -0700, andy wrote:
> Greetings, I followed the Sun tutorial verbatim
> (http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/step6.html)
> and get the UnsatisfiedLinkError displayhelloworld error and I read
> other postings about putting the java file in a package, I even added
> that to my .java file and nothing worked (package foo.bar.test.hw;).
> the hello.dll is in the correct path, so I am confused.. any help
> would be appreciated !!

You can see from the exception that the dll was found, because it
complains about the method itself.

You claim to have tried adding a package statement but the code you
posted does not appear to be in a package. Whatever the case is, you
need to make sure that the "packageness" of the class and the native
methods are in agreement. In other words, it is important that you
re-run javah after making any changes involving package declarations
and check that the method names it generates haven't changed. Make
sure that you don't have several versions of the class file visible
when you run javah, or it might generate the a header for the wrong
version.

Finally, how did you compile the native code? Perhaps your compiler
has mangled the symbol names. This could be the case if you used a C++
compiler or if you are using gcc under Cygwin or similar.

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

andy

unread,
Apr 22, 2002, 11:16:17 AM4/22/02
to
Gordon Beaton <n...@for.email> wrote in message news:<aa0aah$i0j$1...@news.du.uab.ericsson.se>...


So I did what you said, added to my.java file 'package
foo.bar.test.hw;' , removed any old copies of anything, and compiled,
ran javah -jni, etc. etc. All the code looks the same as above still,
exactly what should it look like after I added the package ?

I compiled and created the hello.dll in Visual C++. When I ran the
program, here is my error now, what am I doing wrong ?

D:\j2sdk1.4.0\bin>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
(wron
e: foo/bar/test/hw/HelloWorld)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:509)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.ja
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:246)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:262)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:322)

Gordon Beaton

unread,
Apr 22, 2002, 11:36:44 AM4/22/02
to
On 22 Apr 2002 08:16:17 -0700, andy wrote:
> So I did what you said, added to my.java file 'package
> foo.bar.test.hw;' , removed any old copies of anything, and
> compiled, ran javah -jni, etc. etc. All the code looks the same as
> above still, exactly what should it look like after I added the
> package ?

Please do NOT email me your questions.

I didn't say "add a package declaration". I simply said that any time
you change the package declaration you need to re-run javah and update
the native names accordingly. The symbol names *will* be different.

Also, when you run javah and the class is in a package, you should use
the full name of the class, e.g. "javah foo.bar.test.hw.HelloWorld".

The symbol names it generates for the packaged class will include the
package name, something like this:

Java_foo_bar_test_hw_HellowWorld_displayHelloWorld

For a non-package class, the names will look more like this:

Java_HellowWorld_displayHelloWorld

These changes will be in the header file it generates. Note too that
the header file will get a different name unless you specify the
output filename when you run javah.

> I compiled and created the hello.dll in Visual C++. When I ran the
> program, here is my error now, what am I doing wrong ?
>
> D:\j2sdk1.4.0\bin>java HelloWorld
> Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
> (wron
> e: foo/bar/test/hw/HelloWorld)

This has nothing to do with the native methods, it looks like you have
not put your class files in the right places after compiling with a
package name.

I suggest that you try to get this to work *without* using a package
declaration first.

andy

unread,
Apr 23, 2002, 12:50:15 AM4/23/02
to
ok so what am I supposed to do here to get this to work ? I copied
everything verbatim into visual c++ from the Sun tutorial...I still
get:

D:\j2sdk1.4.0\bin>java HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError:
displayHelloWorld
at HelloWorld.displayHelloWorld(Native Method)
at HelloWorld.main(helloworld.java:9)

again, here is the code:

helloworld.java:


class HelloWorld {
public native void displayHelloWorld();

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

public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}

HelloWorld.h


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


HelloWorldimp.c:


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

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

DLL compile:
cl -Ic:\java\include -Ic:\java\include\win32
-LD HelloWorldImp.c -Fehello.dll

Gordon Beaton <n...@for.email> wrote in message news:<aa1amc$r3h$2...@news.du.uab.ericsson.se>...

Gordon Beaton

unread,
Apr 23, 2002, 2:34:22 AM4/23/02
to
On 22 Apr 2002 21:50:15 -0700, andy wrote:
> ok so what am I supposed to do here to get this to work ? I copied
> everything verbatim into visual c++ from the Sun tutorial...I still
> get:
> D:\j2sdk1.4.0\bin>java HelloWorld
> Exception in thread "main" java.lang.UnsatisfiedLinkError:
> displayHelloWorld
> at HelloWorld.displayHelloWorld(Native Method)
> at HelloWorld.main(helloworld.java:9)
>
> again, here is the code:

At this point you should check that the DLL in fact does contain a
symbol with the correct name, i.e. that your c++ compiler has not
mangled it.

Use a tool like dumpbin or quickview to see exactly what symbols the
DLL comtains (I should add at this point that I am not a windows user
and don't really know about these tools).

There should be no extra characters before or after
"Java_HelloWorld_displayHelloWorld", for example "@8". If there are,
you need to add a compiler option to prevent that from happening.

andy

unread,
Apr 23, 2002, 12:10:15 PM4/23/02
to
Ok, so I must be doing something wrong with my hello.dll compile code,
b/c quickview lists all the functions in hello.dll without any
anomalies like '@' or eveidence of mangling (for example:
Import Table

KERNEL32.dll
Ordinal Function Name

02ad UnhandledExceptionFilter
0174 GetVersion
007d ExitProcess
.......etc )

But no mention of the java function. All I did was copy the code into
in Vis c++ new dll project, built the hello.dll on the following code
-


cl -Ic:\java\include -Ic:\java\include\win32
-LD HelloWorldImp.c -Fehello.dll

I even added to my path and include environment variables in Win2k
d:\j2sdk1.4.0\include and d:\j2sdk1.4.0\include\win32...

any pointers out there ? this is ridiculous !

Gordon Beaton <n...@for.email> wrote in message news:<aa2v9e$e58$1...@news.du.uab.ericsson.se>...

andy

unread,
Apr 23, 2002, 12:16:05 PM4/23/02
to
OK - was I supposed to view the helloworldimp.obj or the hello.dll
file ? because in the .obj I see:

L Ī%Ä<; %.drectvee
.debug$S&#8240; i H B.text8ņ * H
P`.rdata Z @ 0@.debug$S&#8249;h ó H B.debug$T4 H B-defaultlib:LIBCD
-defaultlib:OLDNAMES -editandcontinue
-export:_Java_HelloWorld_displayHelloWorld@8
3 ,D:\bens_programs\jni\Debug\HelloWorldImp.objp

Is this the mangling (@8) ? If so, what can I do to get this java
HelloWorld program to work at this point !!
thanks


Gordon Beaton <n...@for.email> wrote in message news:<aa2v9e$e58$1...@news.du.uab.ericsson.se>...

0 new messages