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 dll from java using JNI (Getting Exception)

843829Jan 20 2010 — edited Jan 20 2010
Hi,
I am using JNI for the first time to call dll from JAVA, so created small program to know the flow.
This is my small java program.

HelloWorld.java
public class HelloWorld {
public native void Hello();

static {
System.load("C:/calldll/HelloWorld1.dll");
System.out.println("Loaded");
}

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

}}

Steps :
1) Compiled java program
javac HelloWorld.java
HelloWorld.class Created.
2) Created Header file using javah utility.
javah -jni HelloWorld
HelloWorld.h created

HelloWorld.h

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

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

#ifdef __cplusplus
}
#endif
#endif


3) Now create HelloWorld.c
Include that header file in this program

#include <jni.h>
#include "HelloWorld.h" --> Header file included
#include <stdio.h>
#include <string.h>

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


Note: JNIEXPORT void JNICALL Java_HelloWorld_Hello
(JNIEnv *, jobject); and
JNIEXPORT void JNICALL Java_HelloWorld_Hello(JNIEnv env , jobject obj) should be same.

Build this program and Got dll in the Debug folder.
in Visual Studio 2005 standard edition HelloWorld.dll

4) copied dll file in the directory C:/calldll/HelloWorld.dll

5) java -D java.library.path= . HelloWorld


C:\calldll>java -D java.library.path = . HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: java/library/path
Caused by: java.lang.ClassNotFoundException: java.library.path
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Please help to solve this problem.


Thank You
Renuka Y.T
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2010
Added on Jan 20 2010
3 comments
481 views