Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calling native method throws java.lang.UnsatisfiedLinkError exception

843829Aug 15 2005 — edited Jun 25 2008
I am trying to run a simple JNI example code from Deitel book.

// JNIPrintMain.java
// Loads the native library, creates a new instance of the
// Java wrapper class, and calls the native methods.

public class JNIPrintMain{

// uses JNI to print a message
public static void main( String args[] )
{
JNIPrintWrapper wrapper = new JNIPrintWrapper();

// call to native methods through JNIWrapper
wrapper.printMessage( "Hello World\n" );
}
}

// JNIPrintWrapper.java
// Allows access to native methods

public class JNIPrintWrapper {

// load library JNIPrintMessage into JVM
static {
System.loadLibrary( "JNIPrintMessage" );
}

// native C++ method
public native void printMessage( String message );

}

// JNIPrintWrapperImpl.cpp
// Implements the header created by java
// to integrate with JNI.

// C++ core header
#include <iostream.h>

// header produced by javah
#include "JNIPrintWrapper.h"

// prints the string given from java
JNIEXPORT void JNICALL Java_JNIPrintWrapper_printMessage
( JNIEnv * env, jobject thisObject, jstring message )
{
// boolean to determine if string is copied
jboolean copied;

// call JNI method to convert jstring to cstring
const char* charMessage =
env->GetStringUTFChars( message, &copied );

// print the message
cout << charMessage;

// release the string to prevent memory leak
env->ReleaseStringUTFChars( message, charMessage );
}

#include <jni.h>
/* Header for class JNIPrintWrapper */

#ifndef IncludedJNIPrintWrapper
#define IncludedJNIPrintWrapper
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNIPrintWrapper
* Method: printMessage
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_JNIPrintWrapper_printMessage
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif


I put in VM parameters -Djava.library.path=<%library path where dll is exist%>
I did all the instructions which are written in the book, but I get an exception
java.lang.UnsatisfiedLinkError: printMessage

at jnisample.JNIPrintWrapper.printMessage(Native Method)

at jnisample.JNIPrintMain.main(JNIPrintMain.java:11)

Exception in thread "main"

What can I do to solve this problem?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 23 2008
Added on Aug 15 2005
28 comments
2,982 views