Java Introduction to JNI Prepared by Humaira Siddiqui.

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Java Native Interface Modified from CS587x Lecture Department of Computer Science Iowa State University.
Writing Native Code for Android Systems. Why ndk There exist large c++ code libraries – E.g., Audio and video compression, e.g., Ogg Vorbis, The LAME.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed.
Administrivia No class next Monday (May 2) Enjoy extra time on P3. ;-) Reading 3 available “Reflections on Trusting Trust”, Ken Thompson No written summary.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Java Native Interface (JNI). JNI Linking Java and C code.
Using Java without BlueJ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling BlueJ projects A BlueJ.
Using Java without BlueJ
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Netprog 2002 Java Intro1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
CSC3170 Introduction to Database Systems
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java Native Interface CS587x Lecture Department of Computer Science Iowa State University.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
What is Android NDK ● A toolset that lets you embed in you app native source code ● C, C++(recently supported December 2010) and assembly(?) ● It is supported.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
Rutgers University Excellence Campaign 2/20/2004 Java Native Interface Tutorial Xiaolin Li Rutgers.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
The Java Programming Language
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
May 9, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part I COMP346/ Operating Systems Revision 1.6 February 9, 2004.
POS 406 Java Technology And Beginning Java Code
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Android JNI and JAR Library JNI Library 1. JNI *.c sources file must include jni header file jni.h #include 2. In Make file CFLAGS must add -I $(NDK_INC)/
CS Software Studio Java Lab 1 Meng-Ting Wang PLLAB, Computer Science Department, National Tsing-Hua University.
Mini University July, 2005 A Little Taste of Java (but don’t tell your folks) (they might think there’s caffeine involved)
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
CS 4244: Internet Programming Network Programming in Java 1.0.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Slide Advanced Programming 2004, based on LY Stefanus's slides Native Methods.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Java 变量类型与 native 变量类型 java 类型 native 类型描述对应的 C 类型 booleanjbooleanunsigned 8 bitsunsigned char bytejbytesigned 8 bitschar jcharunsigned 16 bitsunsigned.
Classes, Interfaces and Packages
C is a high level language (HLL)
Objects First With Java A Practical Introduction Using BlueJ Using Java without BlueJ 1.0.
Using Java without BlueJ BlueJ projects A BlueJ project is stored in a directory on disk. A BlueJ package is stored in several different files.
Object Oriented Programming Lecture 2: BallWorld.
Spring 2006 Special Topics in Computer Engineering: Java Intro 1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Information and Computer Sciences University of Hawaii, Manoa
Intro to ETEC Java.
Internet and Java Foundations, Programming and Practice
CompSci 230 Software Construction
null, true, and false are also reserved.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Java Remote Method Invocation
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Java Introduction to JNI Prepared by Humaira Siddiqui

What is JNI? -1 n JNI stands for Java Native Interface n JNI specifies a communication protocol between Java code and external, native code. n It enables your Java code to interface with native code written in other languages (such as C, C++) n Java native methods are methods declared in your Java code (much like you declare an abstract method), but which are actually implemented in another programming language

What is JNI? -2 n JNI allows Java programmers to n Leverage the improved speed possible with natively compiled code (such as C or assembler). For example, you might need an extremely fast math routine for a scientific application or game program. n Utilize existing code libraries of native code in your Java programs. For example, there might be a really good file compression library written in C. Why try to rewrite it in Java when you can access it (as is) using JNI?

What is JNI? -3 n JNI Drawbacks n Your program is no longer platform independent n Your program is not as robust. If there is a null pointer exception in your native code, the JVM can't display a helpful message. It might even lock up.

What is JNI? -4 n JNI supports n Native methods can create and manipulate Java objects such as strings and arrays. n Native methods can manipulate and update Java objects passed into them (as parameters) n You can catch and throw exceptions from native methods and handle these exceptions in either the native method or your Java application n This almost seamless sharing of objects makes it very easy to incorporate native methods in your Java code

Writing JNI Methods -1 n You write JNI programs for C/C++ by doing the following 1. Create a Java class that declares the native method, contains code for loading the native library. The class should also contain a main method which calls the native method 2. Compile the Java class 3. Run the javah tool with the -jni option to generate a header file for your native C/C++ code 4. Write the C/C++ code (file) which implements the method defined in the header file 5. Compile the header and C/C++ code into a library (a DLL under Windows) 6. Run the Java class (main method)

Writing JNI Methods Write Your Java class package com.cexp.wms.jni.examples; class HelloWorld { public native void displayHello(); //native method { System.loadLibrary("c_library"); } public static void main(String[] args) { HelloWorld hw = new HelloWorld(); hw.displayHello(); } }

Writing JNI Methods Compile your class javac HelloWorld.java

Writing JNI Methods Create the header (.h) file by using the javah tool javah -jni -o HelloWorld.h -classpath. com.cexp.jni.examples.HelloWorld (Note: entire command must be on one line) This command will create a file called HelloWorld.h which contains a C function signature for implementing the native method. The C header file will include, the standard header file for JNI applications.

Writing JNI Methods The header file it creates will look something like /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class HelloWorld */ #ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern "C" { #endif JNIEXPORT void JNICALL Java_com_cexp_wms_jni_examples_HelloWorld_displayHello (JNIEnv *, jobject); #ifdef __cplusplus } #endif

Writing JNI Methods -6 n The displayHello routine has two parameters (standard for all JNI native methods). n JNI Env * -- JNI interface pointer. We can use this pointer to make useful calls like GetStringUTFChars(), which allows us to convert Java Unicode (2 byte) strings to UTF-8 (1 byte) strings. n jobject -- A reference to the (calling) object itself. Similar to this

Writing JNI Methods -7 n JNI native methods can access the following types boolean jboolean string jstring byte jbyte char jchar short jshort int jint long jlong float jfloat double jdouble void

Writing JNI Methods -8 n All of these types can be accessed directly except for jstring, which requires a subroutine call to in order to convert a Java Unicode string (2 bytes) to a C-style char* string (1 byte UTF-8 format).

Writing JNI Methods Write the native C/C++ code which implements the method. Use the same signature that your header file uses. You might name your file something like "HelloWorld.c". #include #include "HelloWorld.h" #include JNIEXPORT void JNICALL Java_com_cexp_wms_jni_examples_HelloWorld_displayHello (JNIEnv *env, jobject obj) { printf("Hello world! I'm here! \n"); return; }

Writing JNI Methods Compile the C/C++ code into a library (a DLL if your code will run under Windows). If you use C++ Builder to compile your library under Windows, make sure you create a DLL project and then add the C/C++ file to it (e.g. HelloWorld.c). You'll need to add the following to your compiler's include path: \javadir\include \javadir\include\win32 Be sure to name your project c_library so the DLL it creates will be named c_library.dll.

Writing JNI Methods If you are compiling your C library under Windows using Visual C++, you do the following: cl -Ic:\javadir\include -Ic:\javadir\include\win32 -LD HelloWorld.c -Fec_library.dll (Note: entire command must be on one line) This creates the file, c_library.dll

Writing JNI Methods If you are compiling your C library under Solaris, you do the following: cc -G -I/javadir/include -I/javadir/include/solaris \ HelloWorld.c -o c_library.so This creates the file, c_library.so

Writing JNI Methods Run the Java class (main method). Switch to the directory just above the "com" directory and run the following: java -classpath. com.cexp.wms.jni.examples.HelloWorld You should see "Hello world! I'm here!" appear on the screen! If you see a "java.lang.UnsatisfiedLinkError" error message, then Java couldn't find your shared library. Either add the directory your library (DLL) resides in to your Java runtime library path OR copy the library file to the working directory of your Java program.

JNI and Fortran n You can invoke Fortran code from your Java applications n To do that, you write some Java JNI code to invoke C/C++ methods in a DLL, and write those C/C++ methods to invoke the Fortran code you want to run n It sounds complicated, but the runtime overhead isn’t as bad as you might think JNI (Java program) C/C++ “bridge” DLL Fortran program

JNI Summary n JNI can be a little tedious the first time you try it, but in the scheme of things it isn't that complicated. n Use JNI to n leverage existing code (that you don't want to port to Java) n Solve problems that Java isn't suited for (e.g., when you need the speed of C or assembler, or when you need to write low level code to communicate directly with hardware