Java Native Interface: CS587x Lecture Department of Computer Science Iowa State University
Java Native Interface: CS587x Lecture Department of Computer Science Iowa State University
Java Native Interface: CS587x Lecture Department of Computer Science Iowa State University
CS587x Lecture
Department of Computer Science
Iowa State University
Introduction
What is native method
What is Java Native Interface (JNI)
Why we use JNI
How to use JNI
Embedding C in Java
Using Java features from C
Embedding the VM
What is Native Method
Functions written in a language other than
Java
They could be C, C++, or even assembly
What is JNI
Java interface to non-Java code. It is Java's link to the
"outside world"
Native methods are compiled into a dynamic link library (.dll,
.so, etc.)
OS loads and links the library into the process that is running
the Java Virtual Machine
Part of the Java Developer Kit(JDK), serves as a glue
between java side and native side of an application
Allows Java code that runs inside a Java Virtual Machine (JVM)
to interoperate with applications and libraries written in other
programming languages, such as C, C++, and assembly
JNI Overview
Why Use JNI
Some functionality are not provided by java
Low-level drives/devices specific to OS
Increase performance by implementing rigorous tasks
in native language
Java is slow in general and may not be suitable for some
functions (e.g., image compression and decompression)
Try to use existing legacy library and could not afford to
rewrite it in java
JNI can be used as a wrapper of these legacy codes
Can slowly migrate legacy code to a newer platform
Improve efficiency of integration
TCP/IP sockets can be used, but there are overhead in data
transmission
Justification
Pros:
Reuse: allows access to useful native code
Efficiency: use best language for the task
Cons:
Applets: doesn't work as they're mobile
Portability: native methods aren't portable
Extra work: javah, create shared native
libs
Interactions with Native Code
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject);
#ifdef __cplusplus
} The JVM reference The calling object
#endif
#endif
HelloWorldImp.c
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
popeye (Linux)
cc -shared -I/usr/java/j2sdk1.4.1_04/include \
-I/usr/java/j2sdk1.4.1_04/include/linux \
HelloWorldImpl.c -o libhello.so
Run the Program
Command:
java HelloWorld
Result:
Hello World!
Possible exceptions:
java.lang.UnsatisfiedLinkError: no hello in shared
library path at
java.lang.Runtime.loadLibrary(Runtime.java) at
java.lang.System.loadLibrary(System.java) at
java.lang.Thread.init(Thread.java)
/* correct way */
JNIEXPORT jint JNICALL Java_IntArray_sumArray(JNIEnv *env, jobject obj,
jintArray arr)
{
int i, sum = 0;
class FieldAccess
{
static int si; /* signature is “si” */
String s; /* signature is “Ljava/lang/String;";
} /* run javap -s -p FieldAccess to get the signature */
Example of Call:
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid = (*env)->GetMethodID(env, cls, “hello”, “(I)V”);
(*env)->CallVoidMethod(env, obj, mid, parm1);
Garbage Collection Issues
Only Arrays and explicitly globally
created objects are “pinned” down and
must be explicitly released
Everything else is released upon the
native method returning
Thread Issues
The JNI interface pointer (JNIEnv *) is
only valid in the current thread
You must not pass the interface pointer
from one thread to another
You must not pass local references from
(locking is needed)
Synchronization
Synchronize is available as a C call
Wait and Notify calls through JNIEnv do work and
are safe to use
Could use native threading operations for native to
native threading, but this may cost portability
In java: In C:
synchronized (obj) (*env)->MonitorEnter(env, obj);
{ ...
/* synchronized block */ /* synchronized block */
...
} (*env)->MonitorExit(env, obj);
Embedding a VM in C
Just a special kind of Java Call from C
(see reference)
You get a pointer into your resulting
environment and by the VM are treated
as a native method
With the exception you never “return”
so it is your responsibility to do
everything globally
References
http://java.sun.com/docs/books/tutorial/native1.1/
http://java.sun.com/j2se/1.3/docs/guide/jni/spec/jniTOC.doc.html
Second Homework
Assigned on Monday, Oct. 3, 2005
CmdAgent Server
RMI
(java) (java)
Send commands and get result back