- How to install Java SDK 1.6 on Ubuntu (manual)
- Download the Java JDK 1.6 from Sun's site. http://java.sun.com/javase/downloads/index.jsp
- Select the Java SE Development Kit (JDK)
- This leads to a page allowing us to select the Operating System, usually we just choose "Linux".
- 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
- Once the newly downloaded file is here, we need to extract it to a folder named "jdk1.6.0_21" (for example).
- It's better then to move Jdk1.6.0_21 folder to "/usr/local/" which is the standard folder for programs or GUI console.
- We will proceed to add the JAVA_HOME variable to the system. To do this, open bash.bashrc file with the following command:
- Open a new console and run the command:
- 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.
- HelloWorlo.java: main function to call native method
- Compile this class and generate class file "HelloWorld.class":
- Generate the header for native method:
- Write implementation C++ file (HelloWorld.cpp): HelloWorld.cpp: The implementation file for native method.
- 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:
- Run this example as following:
$sudo vi /etc/bash.bashrcAt the end of the file put the following instructions.
export JAVA_HOME=/usr/local/jdk1.6.0_21 export PATH=$JAVA_HOME/bin:$PATHThis makes sure that the JAVA_HOME variable is always available in the system.
$java -versionTo 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)
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").
javac HelloWorld.java
javah -jni HelloWorldThe 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
#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);
}
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.
java -Djava.library.path=. HelloWorldThe result like this:
Hello! Ryan Cho


0 意見:
張貼留言