Today I configured the Netbeans IDE to work with Java Native Interface (JNI).
- Install JDK
- Install Cygwin (or) MinGW&MSYS
- Note: While installing please choose the destination directory name without spaces
- I have installed NetBeans IDE All Version, because I thought, I will need all the technologies. That is different approach.
- Set the environment variables like JAVA_HOME and PATH with Java Installation Directory and JDK bin, Cygwin/MinGW&MSYS bin directories respectively
- Cygwin/MinGW integration with NetBeans
- Configure the C/C++ with Tools MinGW bin directory, which automatically fills the corresponding compilers
- Create a sample java application with the following detail
- Clean and Build the java project
- In Command Prompt, Navigate to Netbeans Project Directory
- cmd> cd Netbeans/ProjectDir
- Create the Java header file
- cmd> javah -o JavaApp1.h -classpath JavaApp1\build\classes javaapp1.JavaApp1
- Create a C/C++ Dynamic Library Project in Netbeans (In this example, it is CppDll1)
- Change some of the project properties
- Make sure that, you have downloaded and configured the proper Cygwin/MinGW with exact Architecture 32bit/64bit
- Make sure that you are using proper gcc for 32bit/64bit dll creation
- Linker must be of same architecture, 32bit/64bit
- Include the Java header file, which we have created in Java Project.
- Right click on the "Source Files"-> Select "Add an Existing Item"
- Select the created header file from Project Home Directory
- Manually copy the created header file from Project home directory to CppDll directory
- Create a C Source file in "Source Files" in CppDll1 project
- Clean and Build the C code. I have faced the following issue.
- Issues Faced:
- I am using Windows 7 64-bit and installed Jdk 64-bit. But initially I have installed MinGW 32 bit and it is generating only 32 bit DLL which is not suitable to run the Java application in NetBeans.
- Downloaded MinGW 64 bit and configured in the NetBeans C/C++ Options
- Copy the .dll file path
- In Java project, open the main class and add the following content
- Clean and Build the project
- Run the Java project to see the final JNI output.
Please send your feedback to firstname.lastname@example.org