This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Answer Java and DLL ( interesting enough )
Posted by Kishori Sharan on August 03, 2000 at 11:14 PM
Hi Here is the procedure to call a C++ dll functions from java. I have used this procedure and it works fine. 1. Create a class as follows declaring the method of C++ as native. //HelloCPlus.java class HelloCPlus { public native static void Hello ( ); /* Let us make sure dll is loaded when we call the Hello () static method. you can declare Hello () as non-static too. But in that case you have to make sure that you call loadLibrary ( ) before calling the Hello() method. You may call it in one of the constructors +/ static { System.loadLibrary ( "HelloCPlus" ) ; } } //. end of HelloCPlus.java 2. Now compile this HelloCPlus.java using javac HelloCPlus.java on command as you compile any other java program. 3. Now we will create the header file. Use javah HelloCPlus on command line which will create a file HellocPlus.h as follows // HelloCPlus.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class HelloCPlus */#ifndef _Included_HelloCPlus #define _Included_HelloCPlus #ifdef __cplusplus extern "C" { #endif /* * Class: HelloCPlus * Method: Hello * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloCPlus_Hello (JNIEnv *, jclass); #ifdef __cplusplus } #endif #endif // end of HelloCPlus.h 4. Now you have to create the actual function Hello ( ) in c++. But the function declaration won't be as simple as void Hello ( ). You can take the function declaration in c++ function from HellocPlus.h. I am writing your actual C++ function // HelloCPlus.cpp #include "HelloCPlus.h" #include #include extern "C" /* You must say extern "C" if you are writing C++ function. However, you have to exclude it for c function Note from HelloCPlus.h we took the function declaration which prefix the actual c++ Hello () function name with java_HelloCPlus_. Also note that we have changed the JNIEnv * to JNIEnv* env and jclass to jclass c1*/ JNIEXPORT void JNICALL Java_HelloCPlus_Hello (JNIEnv* env, jclass c1) { cout << "Hello Java \n --- From c++ \n" ; } 5. Now you have to make a dll out of .cpp file . If you have MS VC++ then go to DOS prompt and run the following .bat file C:>vcvars32.bat which is located in your_vcplus_home\vc\bin directory. When you run this .bat file you may get error like "out of environment space" as I was getting initially. If you get this error then open vcvars32,bat using c:> edit vcvars32.bat and try to change the PATH variable to something shorter. You may choose to remove the last %PATH%. This hekped me getting rid of that error. Once the above .bat file runs successfully you can use following command to create the dll. c:>cl -Ic:\java_home\include -Ic:\java_home\include\win32 -LD HelloCPlus.cpp -FeHelloCPlus.dll Here: cl ( it is CL and C1(one ) ), it is located in vc_home\vc\bin\cl.exe c:\java_home : By java_home I mean the directory where you have jdk installed on your computer. If your jdk directory is c:\jdk1.3 then replace the java_home by jdk1.3 -I, -LD, -Fe are switches and you have to type as mentioned. The above command will create ( if you are lucky enough to get it work first time ) a dll named HelloCPlus.dll. 6. Now your C++ function is ready to be called. Create a java program as follows // CallCPlus.java class CallCPlus { public static void main ( String[] args ) { HelloCPlus.Hello ( ) ; } } // end of CallCPlus.java
Now compile CallCPlus.java using c:>javac CallCPlus.java and then run it c:\java CallcPlus and it will print Hello Java ---From C++ Thanx Kishori
Replies:
|