December 27, 2011

Java Native Interface (JNI) in Netbeans with C/C++ and Java Hello World Sample Source Code

Hi all,

Today I configured the Netbeans IDE to work with Java Native Interface (JNI).

  1. Java Development Kit (JDK)
  2. Cygwin (or) MinGW & MSYS
  3. NetBeans IDE with Java and C/C++ Bundle

  • 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

Source Code:

  • Create a sample java application with the following detail

package javaapp1.JavaApp1;

public class JavaApp1
public static void main(String [] args)
  new JavaApp1.nativePrint();

private native void nativePrint();

  • 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
    • (OR)
    • Manually copy the created header file from Project home directory to CppDll directory
  • Create a C Source file in "Source Files" in CppDll1 project
 #include "JavaApp1.h"

JNIEXPORT void JNICALL Java_javaapp1_JavaApp1_nativePrint(JNIEnv *env, jobject obj)
    printf("\nHello World from C\n");  
  • 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.
    • Resolution:
      • 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
package javaapp1.JavaApp1;

public class JavaApp1
  System.load("dll file path");
public static void main(String [] args)
  new JavaApp1.nativePrint();

private native void nativePrint();
  • Clean and Build the project
  • Run the Java project to see the final JNI output.


Please send your feedback to
Post a Comment

Featured Post

Java Introdcution

Please send your review and feedback to