java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Simple example of using the Java Native Interface E-mail
User Rating: / 117
PoorBest 

This tip shows you how to program a simple example of using the Java Native Interface. We will write a Java application that calls a C function to print "Hello World!".

The following steps are needed to develop this example:

  • Creating a class (HelloWorld.java) that declares the native method.
  • Using javac to compile the HelloWorld source file.
  • Using javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation.
  • Writing the C implementation (HelloWorld.c) of the native method.
  • Compiling the C implementation into a native library, creating Hello-World.dll or libHello-World.so.
  • Running the HelloWorld program.
  1. Creating a class (HelloWorld.java) that declares the native method
  2. We begin by writing the following program in the Java programming language. The program defines a class named HelloWorld that contains a native method, print.

     class HelloWorld {

         static {
             System.loadLibrary("HelloWorld");
         }

         private native void print();

         public static void main(String[] args) {
             new HelloWorld().print();
         }
     }

    The first part of the class definition is a static initializer that loads the native library containing the implementation of the print native method. This is followed by the declaration of the print native method and a main method that instantiates the Hello-World class and invokes the print native method for this instance.

  3. Using javac to compile the HelloWorld source file
  4. After you have defined the HelloWorld class, save the source code in a file called HelloWorld.java. Then compile the source file using the javac compiler as follows:

     javac HelloWorld.java
    

    This command will generate a HelloWorld.class file in the current directory.

  5. Using javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation
  6. Next we will use the javah tool to generate a JNI-style header file that is useful when implementing the native method in C. You can run javah on the Hello-World class as follows:

     javah -jni HelloWorld
    

    The command shown above generates a file named HelloWorld.h.

  7. Writing the C implementation (HelloWorld.c) of the native method
  8. The JNI-style header file generated by javah helps you to write C or C++ implementations for the native method. The function that you write must follow the -prototype specified in the generated header file. You can implement the Hello-World.print method in a C file HelloWorld.c as follows:

     #include <jni.h>
     #include <stdio.h>
     #include "HelloWorld.h"
     
     JNIEXPORT void JNICALL 
     Java_HelloWorld_print(JNIEnv *env, jobject obj)
     {
         printf("Hello World!\n");
         return;
     }
  9. Compiling the C implementation into a native library, creating Hello-World.dll or libHello-World.so
  10. Now that all the necessary C code is written, you need to compile Hello-World.c and build this native library.

    Different operating systems support different ways to build native libraries. On Solaris or Linux, the following command builds a shared library called libHello-World.so:

     cc -G -I/java/include -I/java/include/solaris 
         HelloWorld.c -o libHelloWorld.so
    

    The -G option instructs the C compiler to generate a shared library instead of a regular Solaris executable file. Because of the limitation of page width in this book, we break the command line into two lines. You need to type the command in a single line, or place the command in a script file. On Win32, the following command builds a dynamic link library (DLL) HelloWorld.dll using the Microsoft Visual C++ compiler:

     cl -Ic:\java\include -Ic:\java\include\win32 
         -MD -LD HelloWorld.c -FeHelloWorld.dll
    

    The -MD option ensures that HelloWorld.dll is linked with the Win32 multithreaded C library. The -LD option instructs the C compiler to generate a DLL instead of a regular Win32 executable. Of course, on both Solaris/Linux and Win32 you need to put in the include paths that reflect the setup on your own machine.

  11. Running the HelloWorld program
  12. Because the HelloWorld class contains its own main method, you can now run the program on Solaris or Win32 as follows:

     java HelloWorld
    

    You should see the following output:

     Hello World!
    

    It is important to set your native library path correctly for your program to run. The native library path is a list of directories that the Java virtual machine searches when loading native libraries. If you do not have a native library path set up correctly, then you see an error similar to the following:

     java.lang.UnsatisfiedLinkError: no HelloWorld in library path
             at java.lang.Runtime.loadLibrary(Runtime.java)
             at java.lang.System.loadLibrary(System.java)
             at HelloWorld.main(HelloWorld.java)
    

    Make sure that the native library resides in one of the directories in the native library path. If you are running on a Solaris system, the LD_LIBRARY_PATH environment variable is used to define the native library path. Make sure that it includes the name of the directory that contains the libHelloWorld.so file. If the libHelloWorld.so file is in the current directory, you can issue the following two commands in the standard shell (sh) or KornShell (ksh) to set up the LD_LIBRARY_PATH environment variable properly:

     LD_LIBRARY_PATH=.
     export LD_LIBRARY_PATH
    

    If you are running on a Windows 95 or Windows NT machine, make sure that HelloWorld.dll is in the current directory, or in a directory that is listed in the PATH environment variable.

    In Java 2 SDK 1.2 release, you can also specify the native library path on the java command line as a system property as follows:

     java -Djava.library.path=. HelloWorld
    

    The "-D" command-line option sets a Java platform system property. Setting the java.library.path property to "." instructs the Java virtual machine to search for native libraries in the current directory.


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.