Re: how to write cpp file which has the native code needed in java file?
Wednesday, August 7, 2002 06:28 AM
What I would have done, is to create a 'wrapper' class that use the existing c++ code you have created.
1) Decide what functions/methods that you want to use in your Java program.
2) Create a Java class with the methods you need declared as 'native'.
3) Compile the java class.
4) Generate the c++ header-files with javah.
5) Write the code that wraps/uses the existing code that you have, by including libraries, source-files and so on.
The c++ project
If you use Visual Studio, you can create a 'Win32 Dynamic-Link Library' project. Also choose the 'simple DLL project' option.
Include the header-file generated by javah, in your file with #include.
Open 'Tools'-->'Options'-->'Directories' and add the %JAVA_HOME%/include and %JAVA_HOME%/include/win32 directories to the include path.
The java client
Create a client that uses the java-class with the native methods.
Make sure to use System.loadLibrary("myDLL"); to load the library.
You can put the dll in PATH, e.g. c:windowssystem32, and java will be able to find it.