Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java Native Interface Application Notes Da Ke 4/5/2009 ECE 480 Spring 2009, Design Team 3 Department of Electrical and Computer Engineering Michigan State University Abstract Java is one of most widely used programming language today due to its openness, ability to scale up and portability. The language is design to have a simpler object model and fewer low-level facilities like pointer methods in C/C++. Sometime this could impose a limitation on what a program can do. This application note will explore various ways of implementation to allow Java interact with some programs that are written in lower level language. Keywords: Java, JVM, JNI, Linux, Windows, Operating System, Native interface, Native code Introduction Most modern high level programming languages today are capable of handling what once trivial tasks for developers like memory management. However, this doesn't come without cost. A Java application does not interact with underlying system infrastructure directly instead it first compile source code to bytecode and run on Java virtual machine which itself is an application that run on top of the operating system. The Java virtual machine acts like a mediator between the application and the operating system. Most modern implementations of Java Virtual Machine does include several interfaces and libraries that we can utilize to allow Java applications to perform some crucial system level tasks and even speed up the execution of the program by handing over some more resource-intense operations to programs that are running natively on the machine. Objective This application note will explain different approaches to implement system level functions using Java Programming Language. The goal is for the reader with basic understanding of Java to understand different ways of interacting with operating system in Java and what are the trade-offs. Background The ability to access system resource is essential for programs that are design to be flexible and expandable. Approaches Java Native Interface (JNI) http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp16696 JNI for C/C++ Program Most modern java compiler provides an interface library jni.h that allows C/C++ program to access and manipulate Java objects. This is all done by C/C++ memory pointers as shown in figure 1. JNI allows the programmer to pass different Java objects as parameters to C/C++ program and gets the return value from the native program converts it back to the java object. Figure 1: JNI Pointers and Functions Figure 2: Java Native Interface illustration in C/C++ Get it to work There are 5 steps that we need to follow: 1. Write the Java Code 2. Compile the Java Code 3. Create the header (.h) File 4. Implement the Native Method 5. Create a Shared Library Alternative Approach: Java Runtime Object Java also provides another simple and easy interface to interact with operating system – the Runtime object. The Runtime object is a special object that associated with current Java application. It’s able to send commands as a string to system command shell. The command shell is different across different operating system. Figure 3: Java Runtime Command Execution Illustration Conclusion A Java application with system-specific implementation usually scarifies the portability of the application in exchange for the efficiency of the program. However, there are different ways to go around that. For example, you can provide different implementations of native code for various platforms and let Java Virtual Machine decide which implementation to use. This way we can have a relative more efficient application while still able enjoy many great features provides by Java.to implement many general methods on top of Java. References: JNI Design (Sun Microsystem): http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html Java Runtime Object Reference (Sun Microsystem): http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html JNI in Wikipedia: http://en.wikipedia.org/wiki/Java_Native_Interface