how to write cpp file which has the native code needed in java file?
1 posts in topic
Flat View  Flat View

Posted By:   Mousumi_Das
Posted On:   Wednesday, August 7, 2002 02:48 AM

I am a new user of native mathod.
I have different c++ programs and MFC application programs.
I am facing problem in writting native codes in the existing C++ files and MFC application programs which programs are to be converted into dll files and loaded in java programs to call these files from java. Can I get anywhere good tutorial for that or can you suggest a good book for that.
Now I am reffering to the site..

but still not able to write correct c++ programs...
Can you help in this regard.

Re: how to write cpp file which has the native code needed in java file?

Posted By:   Anonymous  
Posted On:   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.
About | Sitemap | Contact