Skip to content

Steps of using Java Native Interface (mix between Java & C/C++ codes)

License

Notifications You must be signed in to change notification settings

HouariZegai/JNIExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JNI Example

Steps of using Java Native Interface (Call C/C++ methods from Java)

License MIT Java 8 Open Source Love

Tools/Programs we need in this tutorial

Step I: Make Java Code for calling C/C++ method

  1. Create a Java Project

  2. Create a class named Hello.java

  3. Write the head of method contains native keyword that describe the functions/methods in C++ (like this)

  4. Compile the Hello.java class using the IDE or by typing this command in the folder that's contain the class:

    javac Hello.java
    

    Note: If javac is not recognized, you need to add the JDK path to the "system environment variable"

Step II: C/C++ Part

  1. Generate the C/C++ JNI header by typing this command:

    javah Hello
    

    And you will see the Hello.h file generated (like this)

  2. Create new project of type Dynamic Link Library on the CodeBlocks IDE

  3. Import the Hello.h file

  4. Create new file named Hello.cpp and make the implementation of the Hello.h method (like this)

  5. Add the jni.h support to C++ compiler:

    1. Right click on the project and click to Build Options

    2. Go to Other compiler options

    3. Add this two lines:

      -I"<JDK path>\include"
      -I"<JDK path>\include\win32"    
      

      In my case:

      -I"C:\Program Files\Java\jdk1.8.0_231\include"
      -I"C:\Program Files\Java\jdk1.8.0_231\include\win32"
      
  6. Build the project

Step III: Import the lib in Java & Execute the code

  1. From C++ project go to the bin\Debug folder and copy the generated library (something like *.dll) and paste it in the java project for example under src/cpplib folder
  2. Now, go to the Hello.java class and import the library using the System.loadLibrary method (see this example here)
  3. Create Main method, call the native java method & run the App

Congratuations you called the C++ method from Java using the Java Native Interface :D

Thank You!

If this tutorial helps you, please ⭐ this repo and share it with others

About

Steps of using Java Native Interface (mix between Java & C/C++ codes)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published