Source code file content

Revision: 2

import
» Project Revision History

» Checkout URL

web-content / trunk / docs / jni / nb6-linux / beginning-jni-linux.html

Size: 12274 bytes, 1 line
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Beginning JNI with NetBeans C/C++ Pack 6.0, Linux</title>
        <link rel="stylesheet" href="http://netbeans.org/netbeans.css">
        <meta name="description" value="A tutorial describing how to compile 
                      and run JNI powered java applications using NetBeans IDE">
        <meta name="author" content="Amit Kumar Saha ">
    </head>
    <body>
        <h1>Beginning JNI with NetBeans C/C++ Pack 6.0, Linux</h1>
        
        <div class="articledate" style="margin-left: 0px;">
            Contributed and maintained by 
            <a href="mailto:amitsaha.in@gmail.com"><i>Amit Kumar Saha</i></a>.
        </div>

<p>This tutorial takes you through the creation of a sample application that 
uses Java<sup><small>TM</small></sup> Native Interface (JNI) code written in the C 
programming language. For the part of the application written in the Java<sup><small>TM</small></sup> 
programming language, you will use 
NetBeans IDE 6.0; for the part written in the C programming language, you will
use NetBeans C/C++ Pack 6.0.

<p>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
6.0.
</p> 
        <p align="center">
            <b>Expected duration: <i>30</i> minutes</b>
        </p>       
<p><b>Prerequisites</b>
</p>
<p>This tutorial assumes you have some basic knowledge of, or programming 
experience with, the following technologies:
</p>
<ul>
<li>
<a href="http://java.sun.com">Java programming language</a>
<li><a href="http://en.wikipedia.org/wiki/C_programming_language">C programming language</a>
<li><a href="http://www.netbeans.org">NetBeans IDE</a>
</ul>
<p><b>Software Required for Tutorial</b>
</p>
<p>Before you begin, you need to install the following software on your 
computer:
</p>
<ul>
<li>J2SE Development Kit (JDK) 6.0 (<a
href="http://java.sun.com/javase/downloads/index.jsp">download</a>)</li>
<li>NetBeans IDE 6.0 (Download the All bundle and customize it to install the
Base IDE, Java SE Pack, and C/C++ Pack) (<a
href="http://dlc.sun.com.edgesuite.net/netbeans/6.0/final/">download</a>)</li>
<li>gcc, make </li>
</ul>
<p><b>Notations Used in Tutorial</b>
</p>
<ul>
<li><tt>&lt;JAVA_HOME&gt;</tt> - the JDK installation directory
<li><tt>&lt;PROJECTS_ROOT&gt;</tt> - directory that contains the Java project 
you create
</ul>

<p><b>Tutorial Exercises</b>
</p>
<p>Exercise 0: Installing and Configuring the Tutorial Environment
</p>
<p>Exercise 1: Setting Up the General Java Application Project
</p>
<p>Exercise 2: Setting Up the C/C++ Dynamic Library Project
</p>
<p>Exercise 3: Building and Running the Application
</p>

<h2>Exercise 0: Installing and Configuring the Tutorial Environment</h2>
<p>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.
</p>
<p><b>Configuring the System</b>: Append the paths to the directories that 
contain the gcc and make utilities to your PATH environment variable. 
</p>
<p><b>Running the Software</b>: Start the NetBeans IDE.
</p>

<h2>Exercise 1: Setting Up the General Java Application Project</h3>
<p>
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.
</p>
<h3>Creating the General Java Application Project</h3>
<ol>
<li>Choose File &gt; New Project. Under Categories, select Java. 
Under Projects, select Java Application. Click Next.</li>
<li>In the Project Name field, type <tt>JNIDemoJava</tt>.</li>
<li>Change the Project Location to any directory on your computer.
(Hereinafter, this directory is referred to as
<tt>&lt;PROJECTS_ROOT&gt;</tt>.)</li>
<li>Leave the Create Main Class checkbox selected and accept the default value for 
the corresponding text field.</li>
<li>
<p>Leave the Set as Main Project checkbox selected. Then click Finish.
</p>
<p>
The IDE creates the <tt>&lt;PROJECTS_ROOT&gt;/JNIDemoJava</tt> project folder.
</p>
<br>
<img src="shots/shot-1.jpg" alt="Screenshot
of the Name and Location page of the New Java Application Wizard">
</li>
</ol>
<h3>Editing the Main Class Source</h3>
<ol>
<li>To open the Main class source in the editor, right-click the Main class 
node and choose Open.</li>
<li>Replace the body of the <tt>main</tt> method with the following:
<pre>
new Main().nativePrint();
</pre>
</li>
<li>Modify the body of the <tt>nativePrint()</tt> method by deleting its 
contents and inserting the native keyword into the method signature as 
follows:
<pre>
private native void nativePrint();
</pre>                          
<p>
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. 
</p>
</li>
<li>Press Ctrl-F11 to clean and build the project. The project should build 
successfully.
</li>
</ol>
<h3>Creating the Native Library Header File</h3>
<ol>
<li>In a terminal window, navigate to the <tt>&lt;PROJECTS_ROOT<&gt;</tt> 
directory.
</li>
<li>Type the following:
<pre class="examplecode">
&lt;PROJECTS_ROOT&gt; javah -o JNIDemoJava.h -classpath JNIDemoJava/build/classes jnidemojava.Main
</pre>                     
<p>
A <tt>JNIDemoJava.h</tt> C header file is generated. This file is required to 
provide a correct function declaration for the native implementation of the 
<tt>nativePrint()</tt> method.</p>
</li>
<li>Switch back to the NetBeans IDE window.
</li>
</ol>
<p><b>Summary</b>
<p>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, you created a C 
header file, which is required later for the native library compilation.</p>
<h2>Setting Up a New C/C++ Dynamic Library Project</h2>
<p>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.
</p>
<p>After you have set up the project, you will create the implementation for 
the native method you declared earlier in the Java part of the application.
<h3>Creating New C/C++ Dynamic Library Project</h3>
<ol>
<li>
<p>
Choose File &gt; New Project. Under Categories, select C/C++. Under 
Projects, select C/C++ Dynamic Library. Click Next.
</p><br>
<img src="shots/shot-2.png"
alt="Screenshot of the Choose Project page of the New Project wizard">
</li>
<li>In the Project Name field, type <tt>JNIDemoCdl-1</tt>.
</li>
<li>In the Project Location field, type the same location as you entered for 
the General Java Application project, <tt>&lt;PROJECTS_ROOT&gt;</tt>. The 
location should be already there as the default value.
</li>
<li>Accept the defaults for all other fields.
</li>
<li>
<p>Leave the Set as Main Project checkbox selected. Then click Finish.
<p>The IDE creates the <tt>&lt;PROJECTS_ROOT&gt;/JNIDemoCdl-1</tt> project 
folder.
</p>
<br>
<img src="shots/shot-3.png"
alt="Screenshot of the Project Name and Location page of the New C/C++ Dynamic
Library wizard">
</li>
</ol>
<h3>Setting Project Properties</h3>
<ol>
<li>Right-click the project node and choose Properties.
</li>
<li>
<p>In the Properties dialog box, expand the C/C++ node and the
GNU C Compiler node, and select the General node. Edit the value of the Include 
Directories property. Set it to <tt>&lt;JAVA_HOME&gt;/include</tt>, 
<tt>&lt;JAVA_HOME&gt;/include/linux</tt>.
</p>
<br>
<img src="shots/shot-4.png" alt="Screenshot of the Project Properties dialog
box and the Debug-Include Directories dialog box">
<br><br>
<p>These settings are required to enable references to the Java <tt>jni.h</tt> library 
from your C code.
</p>
</li>
<li>
<p>Select the Command Line node. Edit the value of the Additional Options 
property. Set it to <tt>-shared -m32</tt>.
</p>
<br>
<img src="shots/shot-5.png"
alt="Screenshot of the Debug-Additional Options dialog box">
<br><br>
<p>
The <tt>-shared</tt> option tells the compiler to generate a dynamic library.
</p>
<p><tt>-m32</tt> 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.
</p>
</li>
<li>
<p>
Expand the Linker node and select the General node.
</p>
<br><img src="shots/shot-6.png"
alt=Screenshot of the Project Properties dialog box and the Debug-Output dialog box
box">
<p>
The goal of this step is to simplify the path of the resulting <tt>.so</tt> file, 
to make referencing it from Java easier for you.
</p>
</li>
<li>Click OK. The defined settings are saved.
</li>
</ol>
<h3>Adding a Header File</h3>
<ol>
<li>Copy the generated <tt>&lt;PROJECTS_ROOT&gt;/JNIDemoJava.h</tt> header file 
to the C/C++ Library project directory,
<tt>&lt;PROJECTS_ROOT&gtl/JNIDemoCdl-1</tt>.
</li>
<li>
<p>In the Projects window, right-click the Source Files node
of the <tt>JNIDemoCdl-1</tt> project and choose Add Existing Item. 
Select the <tt>HelloWorldNative.h</tt> file.
      The <tt>JNIDemoJava.h</tt> file appears under Source Files.
</p>
<br>
<img src="shots/shot-7.png">
</li>
</ol>
<h3>Implementing a Method</h3>

<ol>
<li>Right-click the Source Files node and choose New &gtl Add Empty C File. Type 
<tt>JNIDemo</tt> in the File Name field, and click Finish. The editor opens 
the <tt>JNIDemo.c</tt> file.
</li>
<li>Edit the <tt>HelloWorldNative.c</tt> file by typing the following code:
<pre class="examplecode">>              
#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");
    
}                    
</pre>
</li>
<li>
Right-click the <tt>JNIDemoCdl-1</tt> project node and choose Build Project. The 
Output window displays <tt>Build successful. Exit value 0.</tt>
</li>
</ol>
<h3>Summary</h3>

<p>In this exercise you created a new C/C++ Dynamic Library, specified its 
location, and configured it to be able to build a JNI implementation of your 
Java method. You added the generated header file for the native method you 
declared in the Java application, and implemented it.
</p>
<h2>Building and Running the Application</h2>
<p>
In this exercise, you will perform some final alterations to the Java part of 
the application. These changes are required to ensure the Java part properly 
loads the native library you  compiled in the previous exercise. After that you 
will compile and run the resulting application.
</p>
<h3>Configuring the Java Project</h3>
<ol>
<li>Open the <tt>Main.java</tt> file in the editor.
<li>Add the following initialization code:
<pre class="examplecode">
static {
        System.load("<i>PROJECTS_ROOT</i>/JNIDemoCdl/dist/JNIDemoCdl.so");
       }
</pre>                      
</li>
</ol>
<h3>Running the Application</h3>
<ol>
<li>To set the <tt>JNIDemoJava</tt> Java project as the main project, right-click the 
project node and choose Set As Main Project.</li>
<li>Press F6 to run the application.
The program should execute correctly and the Output window should display:
<pre class="examplecode">>
Hello World from C
BUILD SUCCESSFUL (total time: 0 seconds)
</pre>
<img src="shots/shot-11.png">
</li>
</ol>
<h3>Summary</h3>
<p>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.
</p>
<h2>Next Steps</h2>
<p>You can download the sources for this tutorial from 
<a href="files/JNIDemo.tar.bz2">here</a>.
</p>
<p>
You can use the following documents to get more information:
<ul>
<li><a href="http://www.netbeans.org/kb/60/cnd/quick-start.html">
C/C++ Support Quick Start Tutorial</a>
<li><a href="http://en.wikipedia.org/wiki/Java_Native_Interface">Java Native Interface</a>
</ul>
</body>
</html>

Project Features

About this Project

CND was started in November 2009, is owned by DimaZh, and has 198 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close