当前页面: 开发资料首页 → Netbeans 专题 → Beginning JNI with NetBeans? CC++ Pack 5.5, Part I
摘要: The tutorial will guide 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
The tutorial will guide 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 first part of the tutorial that covers the creation of a sample JNI application in the Microsoft® Windows® operating system.
Expected duration: 30 minutes
This tutorial assumes you have some basic knowledge of, or programming experience with, the following technologies:
Before you begin, you need to install the following software on your computer:
This exercise will guide you through the process of installing and
configuring the required software. While the installation of JDK,
NetBeans IDE and NetBeans C/C++ Pack is pretty trivial, Cygwin
requires special attention.
After you install the software, some minor changes to the system
configuration should be applied.
Figure 1
Figure 2
Figure 3
Add the <CYGWIN_HOME>\bin directory path to the PATH environment variable value.
Start the NetBeans IDE.
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.
Figure 4
Figure 5
new Main().nativePrint();
Figure 6
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.
<JAVA_HOME>\bin\javah.exe -o HelloWorldNative.h -jni -classpath <PROJECTS_ROOT>\HelloWorld\build\classes helloworld.MainA HelloWorldNative.h C header file is generated. It is required to provide correct function declaration for the native implementation of the nativePrint() method.
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.
This exercise will lead you through the creation of the native part of the sample application. You will create the C++ Dynamic Library project and configure it to be able to build the 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.
Figure 7
Figure 8
Figure 9
Figure 10
Figure 11
Figure 12
#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 13
Figure 14
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.
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.
static { System.load("<PROJECTS_ROOT>\\HelloWorldNative\\dist\\HelloWorldNative.dll"); }
Figure 15
Hello World from C BUILD SUCCESSFUL (total time: 0 seconds)
Figure 16
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.
You can download the sources for this tutorial from here.
You can use the following documents to get more information: