Professional Documents
Culture Documents
Lecture 7
Source: http://electrofriends.com/articles/jni/jni-part1-java-native-interface/
Java and JNI
Java applications do not run directly on the hardware, but
actually run in a virtual machine.
Java source code is compiled to a bytecode file and it is
executed by the virtual machine. This makes Java cross-
platform capability.
However, the cross-platform capability limits its
interaction with the local machine's internal components,
making it difficult to use the local machine instructions to
run an existing software library.
JNI make Java code and native code (C/C++) collaborate
and share resources.
Java Compilation
Step 1: Write Java source code Editor
Output
JNI Compilation
Step 1: Write Java source code Editor
Source code with native declaration (xxx.java)
Step 2: Compile Java source code
to generate the bytecode file and
Java Compiler
C++ header file • Bytecode file (xxx.class)
• C++ header file (xxx.h)
Step 3: Write C++ native code
C++ native code (cxx.cpp)
Step 4: Build shared library file
Shared library file (cxx.dll)
class Happy {
public native void printText (); 1 [native method declaration]
static {
[load 2 System.loadLibrary ("happy");
the sam
} en
native am
code e
public static void main (String[] args) {
library] Happy p1 = new Happy ();
p1.printText ();
}
} // happy.cpp 3 [implementation of native method in C++ code]
#include <iostream>
#include "Happy.h"
Write Java
Hello.java
source code
JNI Example: Hello World!
Write Java
Hello.java javac
source code
Hello.h Hello.class
JNI Example: Hello World!
Write Java
Hello.java javac
source code
Write C++
native code
CHello.cpp
JNI Example: Hello World!
Write Java
Hello.java javac
source code
Write C++
native code
Write Java
Hello.java javac
source code
Error message 2:
Primitive Types and Native Equivalents
Each element in Java language must have a corresponding
native counterpart.
Object Types and Native Equivalents
Object Mappings Example
In the Java source code:
class Book {
private native String read(String prompt);
... da t
at
yp
} eo
data type of return value f in
pu
tp
a ra
me
te r
In the C implementation code:
JNIEXPORT jstring JNICALL Java_Book_read(JNIEnv *, jobject, jstring);
string title;
cout << "\nEnter the new book title: ";
getline(cin, title);
jstring result = env->NewStringUTF(title.c_str()); // to create a new jstring instance and
// store it into variable result
return result;
}
JNI String Functions
The JNI provides functions to obtain the Java string
format (Unicode and UTF-8) as shown below.
JNI Function Description
GetStringChars Obtains or releases a pointer to the contents of a string in
ReleaseStringChars Unicode format. May return a copy of the string.
GetStringUTFChars Obtains or releases a pointer to the contents of a string in
ReleaseStringUTFChars UTF-8 format.
NewString Creates a java.lang.String instance that contains the same
sequence of characters as the given Unicode C string.
NewStringUTF Creates a java.lang.String instance that contains the same
sequence of characters as the given UTF-8 encoded C string.
GetStringLength Returns the number of Unicode characters in the string.
GetStringUTFLength Returns the number of bytes needed to represent a string in
the UTF-8 format.
Extra Exercise (Q1)
// Q1.java
import java.util.Scanner;
class Q1 {
public native void printTest(int num);
static {
System.loadLibrary("CQ1");
}
Q1 ww = new Q1();
ww.printTest(num);
}
}
Extra Exercise (Q1)
// CQ1.cpp
#include "Q1.h"
#include <iostream>
class Q2 {
public native void printPyramid(int num);
static {
System.loadLibrary("CQ2");
}
Q2 ww = new Q2();
ww.printPyramid(num);
}
}
Extra Exercise (Q2)
#include "Q2.h"
#include <iostream>