Click here to Skip to main content
Click here to Skip to main content

JNI Basics - 1

, 10 Sep 2002
Rate this:
Please Sign up or sign in to vote.
Calling native functions which are written in C or C++ from Java, using JNI.


In this tutorial, I will try to explain how to call native functions which are written in C or C++ from Java, using JNI.

What is JNI

JNI is an acronym of Java Native Interface. Using JNI, we can call functions which are written in other languages from Java. Advantages and disadvantages of this method are as follows:


  1. You can use your existing library which was previously written in another language.
  2. You can call Windows API functions.
  3. For the sake of execution speed.
  4. You can call API functions of some server product which is in C or C++, from Java client.


  1. You can’t say "write once run anywhere".
  2. Difficult to debug runtime error in native code.
  3. Potential security risk.
  4. You can’t call it from Applets.

Here we will try to call the MFC function AfxMessageBox() from Java. Our function in C++ will take no arguments and will not return any value.

class Test1
    public static void main(String ar[])
        System.out.println("Hello world from Java");
        Test1 t=new Test1();
    public native void inDll();

In static block, we are loading a DLL named TestDll.dll which should be placed in <WINDIR>/SYSTEM32 directory. I am not saying you cannot put this DLL in other directories, you can, but then you will have to register that path in JVM. Here you can call another method as well, instead of loadLibrary().


I think load() method is better as far as usage is concerned because you can easily place your DLL in any directory.

public native void inDll();

As you can guess, this native keyword specifies that inDll() is a native function which is implemented in the library. Also noticeable, there is no implementation. When we call this function in our code, JVM will look for the function in the loaded DLL (due to static block, DLL will be loaded immediately after the loading of the class) and then call that function. Otherwise it will throw java.lang.UnsatisfiedLinkError exception.

Compile this class normally. Now we will need to generate header file of this class.

You can use javah.exe utility which is included with JDK.

Javah -jni Test1

Header file is generated in current directory with the name Test1.h. Keep this header file safe as we will use this soon.

Now we are going to write a DLL which will contain the implementation of our native function. I have chosen MFC AppWizard (DLL) in project and named my project: TestDll.

Include your class header file which was generated previously using javah.exe in TestDll.h.

Copy following line from Test1.h and paste it at the end of TestDll.h.

JNIEXPORT void JNICALL Java_Test1_inDll(JNIEnv *, jobject);

Change this line to:

JNIEXPORT void JNICALL Java_Test1_inDll(JNIEnv *env, jobject obj)
    AfxMessageBox("Hello World from dll");

Compile this DLL, you will get an error from compiler because compiler was unable to find jni.h file which is included in Test1.h. So include the following two directories in IDE settings.

  • <Drive>:\<JDK Directory>\include
  • <Drive>:\<JDK Directory>\include\win32

Go to Project/Settings, on C/C++ tab, go to Project option, insert following lines at the end:

/I "<Drive>:\<JDK Directory>\include "
/I "<Drive>:\<JDK Directory>\include\win32"

Now compile and generate your DLL, put newly created DLL in system32 directory and then run your class file. It should work properly. If you receive an error like UnsatisfiedLinkError, then recheck your procedure. If you are using any IDE, then try using command prompt. Hope it will work.

In this tutorial, I have tried to explain the basics of JNI. I will write a second tutorial of the series soon in which I will explain how to pass parameters to native functions and return something, along with a practical example.

Waiting for your comments and questions.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Irfan Dawood
Web Developer
Pakistan Pakistan
Irfan Dawood is the junior year student at Department of Computer Science, University of Karachi. His interest includes programming in C++ and Java.

Comments and Discussions

GeneralExcellent and Straight Forward PinmemberHabeeballah Hasnoddin27-Oct-13 1:42 
QuestionIts good article for starter ... Pinmembervijay_vijay216-Jan-13 21:46 
QuestionGreat article, but had an issue with javah on my java class PinmemberRonboFloyd3-Jan-12 8:49 
GeneralMy vote of 3 PinmemberAmilarajans22-Nov-11 6:59 
QuestionError PinmemberGopi268-Oct-11 0:10 
AnswerRe: Error PinmemberMember 1105198228-Oct-14 0:12 
QuestionError PinmemberGopi267-Oct-11 22:09 
GeneralMy vote of 3 PinmemberMazahir Clipwala8-Sep-11 7:01 
QuestionBASIC to JAVA??? Pinmemberniobie28-Sep-09 5:11 
GeneralHi doubt abt JNI in Servlet and JSP PinmemberMember 597963018-Jun-09 20:23 
QuestionJNI problem help Pinmembergmour1-Dec-08 8:03 
Generalit finally works. Good. PinmemberPeter, Chan30-Oct-08 22:33 
QuestionCalling java functions from c++ PinmemberAnkush M Gupta19-Oct-08 3:06 
AnswerRe: Calling java functions from c++ Pinmembersaikrishna35020-Oct-08 0:15 
GeneralRe: But How PinmemberAnkush M Gupta20-Oct-08 0:22 
QuestionExcept dll, others? Pinmemberyulin113-Oct-08 17:56 
AnswerRe: Except dll, others? PinmemberRogerPack14-Jul-10 9:57 
QuestionReading .lib file Pinmembersharad thute1-Jul-08 1:58 
AnswerRe: Reading .lib file PinmemberRogerPack14-Jul-10 9:59 
QuestionHow to call a DLL developed in C++ without re-Implimentation? Pinmemberyasirqau9-Apr-08 21:47 
QuestionCan I call .dll from Java Web App.(JSP, struts) Pinmemberjiraiya1177-Aug-07 23:53 
GeneralNon native (JNI less) access to COM PinmemberVikram RoopChand30-Jun-07 23:28 
GeneralMake it worked Pinmemberbrofirdaus28-Jun-07 16:34 
GeneralRe: Make it worked PinmemberOLEDB_Novice9-Aug-07 5:09 
GeneralRe: Make it worked PinmemberOLEDB_Novice9-Aug-07 5:18 
GeneralC++ application to Java using JNI Pinmemberbrofirdaus24-Jun-07 17:49 
Generalpassing objects in jni Pinmemberkatri23-Mar-07 1:53 
GeneralRe: passing objects in jni PinmemberK.M.Hussain23-Mar-07 6:13 
Questionsome problem PinmemberMember #385999823-Feb-07 23:18 
Generalhelpful Pinmembersheniss13-Oct-06 2:29 
Dear Irfan, Thank u so much...

Questionhelp me out JNI problem Pinmembergood61-Aug-06 4:54 
GeneralUsing Visual Studio 2005 PinmemberWiseHacker14-Jun-06 16:52 
GeneralRe: Using Visual Studio 2005 PinmemberCodeKayak22-Nov-06 12:48 
GeneralRe: Using Visual Studio 2005 Pinmemberfish.soluble22-Feb-07 7:23 
GeneralRe: Using Visual Studio 2005 Pinmemberthebigandroid13-May-08 12:33 
Generalsupermarket simulation Pinmembermccleon3-Mar-06 7:35 
Generalthe client retreive info from the server by pressing a button in java Pinmembermccleon3-Mar-06 7:31 
Generallearning PinmemberProgramgirl27-Jan-06 9:18 
GeneralRe: learning Pinmemberconscript121-Aug-08 4:09 
Generalgetting set up PinmemberProgramgirl27-Jan-06 9:15 
GeneralRe: getting set up PinmemberWiseHacker14-Jun-06 17:17 
GeneralJava call DLL Pinmemberjigneshlakhani9-Jan-06 0:42 
GeneralRe: Java call DLL PinmemberAljechin12-Jan-06 0:36 
GeneralRe: Java call DLL Pinmemberjigneshlakhani12-Jan-06 1:04 
GeneralGood Article.(I have successfully done it.) PinmemberRaksha Dokania19-Dec-05 2:10 
GeneralRe: Good Article.(I have successfully done it.) [modified] PinmemberWiseHacker14-Jun-06 17:23 
GeneralDebug DLL File Pinmemberlvguangchuan18-Oct-05 11:17 
GeneralHelp me in solving Unsatisfiedlinkerror! PinsussAnonymous17-Aug-05 1:00 
GeneralRe: Help me in solving Unsatisfiedlinkerror! PinmemberYanaisama26-Dec-06 6:31 
GeneralRe: Help me in solving Unsatisfiedlinkerror! Pinmemberravindra.java10-Apr-07 23:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 11 Sep 2002
Article Copyright 2002 by Irfan Dawood
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid