Here's my HelloWorld.java
public class HelloWorld
{
public native void displayMessage();
static
{
System.loadLibrary("libhelloworld2");
}
}
Here's main Main.java
public class Main
{
public static void main(String[] args)
{
HelloWorld hello = new HelloWorld();
hello.displayMessage();
}
}
Here's my 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: displayMessage
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayMessage
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Here's my HelloWorld.cpp
/*
* HelloWorld.cpp
*
* Created on: Jul 11, 2008
* Author: panderson
*/
#include <stdio.h>
#include "HelloWorld.h" // this header file was generated by javah
JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
}
Here's the output that I get when I run it
Exception in thread "main" java.lang.UnsatisfiedLinkError: displayMessage
at HelloWorld.displayMessage(Native Method)
at Main.main(Main.java:7)
I'm pretty sure it is finding the DLL file because when it does not find the DLL the error message is as follows
Exception in thread "main" java.lang.UnsatisfiedLinkError: no libhelloworld2 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:993)
at HelloWorld.<clinit>(HelloWorld.java:7)
at Main.main(Main.java:6)
------------------------------------------------------------------
So it seems to be loading the DLL but when it goes to call the method it generates an exception instead.
I used MinGW (g++) to create the DLL. Here's the compilation instructions it generated to create the DLL
**** Build of configuration Debug for project helloworld2 ****
**** Internal Builder is used for build ****
g++ -IC:\Java\jdk1.5\include -IC:\Java\jdk1.5\include\win32 -O0 -g3 -Wall -c -fmessage-length=0 -oHelloWorld.o ..\HelloWorld.cpp
g++ -shared -olibhelloworld2.dll HelloWorld.o
Build complete for project helloworld2
Time consumed: 280 ms.
So I am stumped as to why it is generating the exception. It's a real show stopper.
Any help would be greatly appreciated, thanks.
- Perry
Edited by: perriera on Jul 11, 2008 11:56 AM