• Simple example of using the Java Native Interface


    http://www.java-tips.org/other-api-tips/jni/simple-example-of-using-the-java-native-interface.html

    ————————————————————————————————————————————————————————————

    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.

     

    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    团队冲刺第一阶段第三天
    团队冲刺第一阶段第二天
    团队冲刺第一阶段第一天
    学习进度07
    学习进度06
    结对编程之子数组最大的和(由于一直登不上我的账号,在规定日期内只在我搭档的博客上交了)
    小学四则运算网页版
    团队介绍
    学习进度条05
    111
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2232608.html
Copyright © 2020-2023  润新知