Beginning JNI with NetBeans C/C++ Pack 5.5, Linux
The tutorial will take you through the creation of a sample
application which uses JNI to execute some native code written in
the C programming language. For the Java™ part of the application you
will use NetBeans™ IDE 5.5; for the C part - NetBeans™ C/C++ Pack
5.5.
You will start off by creating a simple Java project, adding a native
method to it and then implementing this method in C using
NetBeans C/C++ Pack 5.5.
This is the second part of the tutorial that covers the creation of a sample JNI application in the Linux operating system.
Expected duration: 30 minutes
Prerequisites
This tutorial assumes you have some basic knowledge of, or programming experience with, the following technologies:
Software Required for Tutorial
Before you begin, you need to install the following software on your computer:
- J2SE Development Kit (JDK™) 5.0 (download)
- NetBeans IDE 5.5 (download)
- NetBeans C/C++ Pack 5.5 (download)
- gcc, make
Notations Used in Tutorial
- <JAVA_HOME> - the JDK installation directory
- <PROJECTS_ROOT> - directory that contains the Java project you create
Tutorial Exercises
Exercise 0: Installing and Configuring Tutorial Environment
This exercise will guide you through the process of performing required configuration changes that should be applied to your system, prior to starting the tutorial.
Configuring System
Append the paths to the directories that contain the gcc and make utilities to the PATH environment variable.
Running Software
Start the NetBeans IDE.
Setting Up General Java Application Project
The goal of this exercise is to create and configure the Java part of the JNI application you will be developing. You will create a new Java application project, initialize its main class and add a native method to this class.
Creating General Java Application Project
Figure 1
Figure 2
The IDE creates the <PROJECTS_ROOT>/HelloWorld project folder.
Editing Main Class Source
new Main().nativePrint();
A compilation error was caused by referencing a non-existing method in Step 2 above and the IDE automatically proposes a fix for this problem.
Figure 3
private native void nativePrint();
The native keyword indicates that the method
has an implementation located in an external native
library, thus the code is going to compile correctly. However
at runtime the library location is not clear. Refer to the Configuring the Java Project section below for details.
The project should build successfully.
Creating Native Library Header File
<JAVA_HOME>/bin/javah -o HelloWorldNative.h -jni
-classpath <PROJECTS_ROOT>/HelloWorld/build/classes helloworld.Main
A HelloWorldNative.h C header file is generated. It is required to provide correct function declaration for the native implementation of the nativePrint() method.
Summary
In this exercise you created a new General Java Application Project, specified its location and defined the package and name of the main class of the project. You also added a new method to the main class and marked it as a method having a native implementation. As a final step we created a C header file which is required later for the native library compilation.
Setting Up New C/C++ Dynamic Library Project
This exercise will lead you though the process of creating the native part of the sample application. You will create the C++ Dynamic Library project and configure it to be able to build JNI code.
After the project has been set up, you will create the implementation for the native method, you have declared earlier in the Java part of the application.
Creating New C/C++ Dynamic Library Project
Figure 4
Figure 5
The IDE creates the <PROJECTS_ROOT>/HelloWorldNative project folder.
Setting Project Properties
These settings are required to enable references to the Java jni.h library from your C code.
Figure 6
The -shared option tells the compiler to generate a dynamic library.
-m32 tells the compiler to create a 32-bit binary. By default on 64-bit systems the compiled binaries are 64-bit, which causes a lot of problems with 32-bit JDKs.
Figure 7
The goal of this step is to simplify the path of the resulting so file, in order to make referencing it from Java easier for you.
Figure 8
The defined settings are saved.
Adding Header File
The HelloWorldNative.h file appears under Source Files.
Implementing Method
The editor opens the HelloWorldNative.c file.
Figure 9
#include <jni.h>
#include <stdio.h>
#include "HelloWorldNative.h"
JNIEXPORT void JNICALL Java_helloworld_Main_nativePrint
(JNIEnv *env, jobject obj)
{
printf("\nHello World from C\n");
}
Figure 10
The Output dialog box displays Build successful. Exit value 0.
Figure 11
Summary
In this exercise you created a new C/C++ Dynamic Library, specified its location and configured it to be able to build JNI implementation of your Java method. You added the generated header file for the native method you have declared in the Java application and implemented it.
Building and Running Application
In this exercise you will perform some final alterations to the Java part of the application. This is required to ensure the Java part properly loads the native library you had compiled in the previous exercise. After that you will compile and run the resulting application.
Configuring Java Project
static {
System.load("<PROJECTS_ROOT>/HelloWorldNative/dist/HelloWorldNative.so");
}
Figure 12
Running Application
The program should execute correctly and the Output dialog box should say:
Hello World from C
BUILD SUCCESSFUL (total time: 0 seconds)
Figure 13
Summary
In this exercise you made some final configuration steps and ran the application to verify that the implementation of the native method comes from the native C library.
Next Steps
You can download the sources for this tutorial from here.
You can use the following documents to get more information:
- Getting Started With the NetBeans C/C++ Development Pack, the NetBeans C/C++ Pack 5.5 tutorial
- Java Native Interface, the Java Native Interface 5.0 specification
