intelliproject logo

Location: Desktop development - Java    License: The Intelliproject Open License (IPOL)

Call native C/C++ functions from Java using JNI

Posted by Silviu Caragea

Describes how to integrate c/c++ native libraries into your Java applications

Skill: Intermediate

Posted: 18/01/2009

Views: 2465

Rating: 5.00 /5

Popularity: 0.00

Sign Up to vote for this article

Introduction

The Java Native Interface (JNI) is a programming framework that allows Java programmers to integrate native code (C/C++/asm) into their Java applications.

The JNI framework lets a native method utilize Java objects in the same way that Java code uses these objects. A native method can create Java objects and then inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code.

JNI is sometimes referred to as the "escape valve" for Java developers because it allows them to add functionality to their Java Application that the Java API can't provide. It can be used to interface with code written in other languages, like C++. It is also used for time-critical calculations or operations like solving complicated mathematical equations, since native code can be faster than JVM code.

Advantages:

  • You can use existing libraries written in other languages.
  • You can call OS API functions.
  • Increasing of execution speed.

Disadvantages:

  • Difficult to debug runtime error in native code.
  • Potential security risk.
  • You can call it just from signed Applets.
  • There is no garbage collection for the JNI side.

There are two different ways to load a native library into a Java application: System.loadLibrary(String) and System.load(String).

The System.loadLibrary method allows us to load a library from the "default" path. System.load allows us to load a library from an absolute path.

When using System.loadLibrary, the only thing we specify is the name of the DLL file we want. The JVM will search for it in the "Java library path". You can see your default "Java library path" with the following syntax:

System.out.println(System.getProperty("java.library.path"));

or

echo %PATH% from start->run->cmd

As you can see the current directory is inserted into the PATH environment variable .

Now lets create a simple java application with 3 native methods imported from a c++ library.

As you can see the native methods are specified using the "native" keyword.

Now we must to compile our application to generate the .class object file. We can do that from our IDE (if we use one) or from command line:

javac MainAppClass.java

After that we must generate the header file (.h) for ours native methods. We can do that using the javah.exe :

Javah - o JniHeaderFile.h -jni -classpath ..\..build\classes\ jniexample.MainAppClass

This will generate a header file called JniHeaderFile.h .

javah.jpg

Creating the C++ dll

If you don't know how to create a dll using Visual c++ you can read here :How to export C++ classes and functions from a DLL

You must include the Java JNI headers path into your IDE. Or you can copy jni.h and jni_md.h into your current project.

After that you must write your implementation for the native methods from your generated header.

After you compile your library you must copy the DLL into your current path or in a path specified in PATH environment variable .

Output :

Enter text:
Prompter
You Entered Prompter
Hello world from VC++ DLL
Opened Window:
Window open: Type a question for help
Window open: SysFader
Window open: Start Menu

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Silviu Caragea

Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject.

He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy.

His programming experience includes:
- C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL)
- Open Source libraries :CURL & Boost
- HTML, CSS
- Java (SE,ME)
- JavaScript, Ajax, Google Web Toolkit (GWT)
- Php, MySQL
-Oracle, PL SQL
- C# .NET
-Objective C, IPhone SDK, Cocoa

Location: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Sign up to post message on the article message board!