Ibraheem Osama Mohamed (ابراهيم اسامه محمد)

Welcome to my blog :)

Java Native Interface

leave a comment »

During I was making a java task that requires windows API I tried to find a solution and I find through Java Native Interface. and I searched a lot to find a tutorial that include all the ways to make interface and the connections.

So I made this post to make easy for any one to make the JNI


Types of JNI.
JNI Primitive type.
Program in details.
Summary and Questions.


●The Java Native Interface (JNI) is a powerful feature of the Java plat
form. Applications that use the JNI can incorporate native code written in programming languages such as C and C++, as well as code written in the Java programming language.
●JNI used when we need to use API in language that aren’t in Java.

Types of JNI

1)Native Library.
2)Native Application.

Native Library 

You can use the JNI to write native methods that allow Java applications to call functions implemented in native libraries. Java applications call native methods in the same way that they call methods implemented in the Java programming language. however, native methods are implemented in another language and reside in native libraries.

Native Application


The JNI supports an invocation interface that allows you to embed a Java virtual machine implementation into native applications.            For example, a web browser written in C can execute downloaded applets in an embedded Java virtual machine implementing.

JNI Primitive Types 

●There are big different between java and C++ in primitive types space in memory like
●int in C takes 16 bits
●int in java takes 32 bits
●So they made an interface that allocate primitive types memory in C as in Java
●JInt in C = int in java
●Jstring in C = String in Java

Let’s get in deep

Objective: Program to show the language Digit Substitution of the Control Panel (CONTEXT-NATIONAL-NONE).
Challenge:The Java API cannot access the windows Control Panel.
Solution: Make JNI with programming language that have API can access the windows like C++.

Steps of JNI

1.Create a class (.java) that declares the native method.
2. Use javac to compile the .java source file, resulting in the class file .class.
3. Use javah -jni to generate a C header file (.h) containing the function prototype for the native method implementation.
4. Write the C implementation (.cpp) of the native method.
5. Compile the Cpp implementation into a native library, creating .dll(recomanded) or .so.
6. Run the  program using the Java runtime interpreter. Both the class file (.class) and the native library (.dll or .so) are loaded at runtime.

Program in details

1)Declare the Native Method

2)Compile the Class

hint : Must write the full path
This command will generate a HelloWorld.class file in the current directory.

3)Create the Native Method Header File

hint :must write the package full package name and the class name , this command is written in the src not in the .class path The name of the header file is the class name with a “.h” appended in the src file.

4)Write the Native Method Implementation

5)Compile the C++ Source and Create a Native Library.(two ways)

a)using command prompt On Win32

b)Using VC we will create a class library not a console then we add the paths of the library

6)Run the Program

Including a line of code that loaded a native library into the program:


JNI With Eclipse.
The Java™ Native Interface Programmer’s Guide and Specification.

  Ibraheem Osama Mohamed
Mobile Developer-Rashdan It || Microsoft Student Partner -Microsoft Egypt


Written by Ibraheem Osama Mohamed

September 29, 2011 at 10:22 pm

Posted in JNI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: