Where shall I put dll file before calling it to the java program?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mousumi_Das
Posted On:   Thursday, July 18, 2002 10:19 PM


I want to call C++ function in the java program. For that I have created java header file, include in the c++ program then created dll file from that. Now i want to call that dll from java program.The java program is




class CallCPlus {

public static void main ( String[] args ) {

HelloCPlus.Hello ( ) ;

}

}

where "HelloCPlus" is the dll file name and "Hello" is the native method name.While compiling the above java file it is giving error "Undefined variable or classname: HelloCPlus". Can you help me in this regard?

Re: Where shall I put dll file before calling it to the java program?

Posted By:   Gautam_Marwaha  
Posted On:   Thursday, July 18, 2002 10:46 PM

Looks like you are new to java and/or jni...

Here's a quick review of what's done in JNI (though you'd be much better off reading the JNI section in the "Java Tutorial" available at http://java.sun.com/docs/books/tutorial/native1.1/index.html). Note the code in the java class:

Java class having a native method, also has main method for running:


class NativeStuff
{
public native void doSomething();

static
{
System.loadLibrary("mylib"); //name of dll is mylib.dll
}

public static void main(String[] args)
{
NativeStuff n = NativeStuff();
n.doSomething();
}
}


NativeStuff.h generated by javah will be as below:


/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class NativeStuff */

#ifndef _Included_NativeStuff
#define _Included_NativeStuff
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL Java_NativeStuff_doSomething
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


mylib.cpp is the native code you will write:


#include "NativeStuff.h"

JNIEXPORT void JNICALL Java_NativeStuff_doSomething(JNIEnv *env, jobject obj)
{
//your code here
}


1. compile, link to create the dll - mylib.dll

2. Run as follows:


java -Djava.library.path=

NativeStuff
About | Sitemap | Contact