2010年9月23日 星期四

Using Java Native Interface on Ubuntu 10.04

從無到有: to achieve Java Native Interface(JNI) on Ubuntu 10.04
  • How to install Java SDK 1.6 on Ubuntu (manual)
    1. Download the Java JDK 1.6 from Sun's site.
    2. http://java.sun.com/javase/downloads/index.jsp 
    3. Select the Java SE Development Kit (JDK)
    4. This leads to a page allowing us to select the Operating System, usually we just choose "Linux".
    5. Then we came to a page which shows the available packages. I click on "jdk-6u21-linux-i586.bin" because this version is a package resembling to a tar.gz
    6. Once the newly downloaded file is here, we need to extract it to a folder named "jdk1.6.0_21" (for example).
    7. It's better then to move  Jdk1.6.0_21 folder to "/usr/local/" which is the standard folder for programs or GUI console.
    8. We will proceed to add the JAVA_HOME variable to the system. To do this, open bash.bashrc file with the following command:
    9. $sudo vi /etc/bash.bashrc
      At the end of the file put the following instructions.
      export JAVA_HOME=/usr/local/jdk1.6.0_21
      
      export PATH=$JAVA_HOME/bin:$PATH
      This makes sure that the JAVA_HOME variable is always available in the system.
    10. Open a new console and run the command:
    11. $java -version
      To which we assume the following result:
      java version "1.6.0_21"
      Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
      Java HotSpot(TM) Server VM (build 17.0-b16, mixed mode)
  • Achieve a simple JNI implementation on Ubuntu (Java call C/C++)
  • Example: To Pass a string from Java to native method (C++) and prints personalized Hello World.
    1. HelloWorlo.java: main function to call native method
    2. class HelloWorld {
          public native void sayHi(String name);
          static
          {
              System.loadLibrary("HelloWorld");
          }
          public static void main(String[] args)
          {
              HelloWorld h = new HelloWorld();
              h.sayHi("Ryan Cho");
          }
      }
      
      We announce our intention to use native method "sayHi" from "HelloWorld" library -- System.loadLibrary("HelloWorld").
    3. Compile this class and generate class file "HelloWorld.class":
    4. javac HelloWorld.java
    5. Generate the header for native method:
    6. javah -jni HelloWorld
      The header file for native method "HelloWorld.h" is generated.
      HelloWorld.h: The header file for native method (automatic generated)
      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class HelloWorld */
      
      #ifndef _Included_HelloWorld
      #define _Included_HelloWorld
      #ifdef __cplusplus
      extern "C" {
      #endif
      /*
       * Class:     HelloWorld
       * Method:    sayHi
       * Signature: (Ljava/lang/String;)V
       */
      JNIEXPORT void JNICALL Java_HelloWorld_sayHi
        (JNIEnv *, jobject, jstring);
      
      #ifdef __cplusplus
      }
      #endif
      #endif
      
    7. Write implementation C++ file (HelloWorld.cpp):
    8. HelloWorld.cpp: The implementation file for native method.
      #include <jni.h>
      #include <iostream>
      #include "HelloWorld.h"
      
      using namespace std;
      
      JNIEXPORT void JNICALL Java_HelloWorld_sayHi
        (JNIEnv *env, jobject o, jstring s)
      {
          const char *str = env->GetStringUTFChars(s, 0);
          cout << "Hello! " << str << endl;
          env->ReleaseStringUTFChars(s, str);
      }
      
    9. There is no garbage collector in C++ world, so we need to do some cleanup. We save this as HelloWorld.cpp and compile it to generate shared library:
    10. g++ -shared -I/usr/local/jdk1.6.0_21/include -I/usr/local/jdk1.6.0_21/include/linux HelloWorld.cpp -o libHelloWorld.so
      !Attention: The path to your java include directory may be different.
    11. Run this example as following:
    12. java -Djava.library.path=. HelloWorld
      The result like this:
      Hello! Ryan Cho

0 意見: