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;
}
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
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)
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.
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>...
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.
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>...
L Ī%Ä<; %.drectvee
.debug$S‰ i H B.text8ņ * H
P`.rdata Z @ 0@.debug$S‹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>...