JNI: Java Native Interface(1/32) Real Time Systems LAB. JNI: Java Native Interface October 31, 2001 Kyung-Yim, Kim.

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Inlining Java Native Calls at Runtime (CASCON 2005 – 4 th Workshop on Compiler Driven Performance) Levon Stepanian, Angela Demke Brown Computer Systems.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Java Native Interface Modified from CS587x Lecture Department of Computer Science Iowa State University.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
Pemrograman Dasar - Data Types1 THINGS & STUFF Identifier, Variable, Constant, Data type, operator, expression, assignment, javadoc.
Java Native Interface (JNI). JNI Linking Java and C code.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Modern Concurrency Abstractions for C# by Nick Benton, Luca Cardelli & C´EDRIC FOURNET Microsoft Research.
1 The Java Virtual Machine Yearly Programming Project.
Java Native Interface CS587x Lecture Department of Computer Science Iowa State University.
Java Introduction to JNI Prepared by Humaira Siddiqui.
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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
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.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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)/
C Lab 1 Introduction to C. Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly!
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
CSC 212 Object-Oriented Programming and Java Part 2.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Slide Advanced Programming 2004, based on LY Stefanus's slides Native Methods.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Memory Management.
CIS 068 JAVA vs. C++ Main Differences CIS 068.
Object Oriented Programming in
Chapter VII: Arrays.
Computer Organization and Design Pointers, Arrays and Strings in C
Objects as a programming concept
Lesson One – Creating a thread
Objectives You should be able to describe: Interactive Keyboard Input
Java Primer 1: Types, Classes and Operators
Quiz 11/15/16 – C functions, arrays and strings
Topic: JAVA .Class File Structure
Programmazione I a.a. 2017/2018.
This pointer, Dynamic memory allocation, Constructors and Destructor
Advanced Programming Behnam Hatami Fall 2017.
Object Based Programming
Topics Introduction to File Input and Output
Chapter 9 Classes: A Deeper Look, Part 1
Java Programming Language
Functions, Part 2 of 3 Topics Functions That Return a Value
Classes and Objects.
Object Oriented Programming in java
Java Programming Language
Chapter 9: Pointers and String
Unit-1 Introduction to Java
Topics Introduction to File Input and Output
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Week 9 - Monday CS222.
Threads and concurrency / Safety
Introduction to C CS 3410.
Introduction to Classes and Objects
Presentation transcript:

JNI: Java Native Interface(1/32) Real Time Systems LAB. JNI: Java Native Interface October 31, 2001 Kyung-Yim, Kim

JNI: Java Native Interface(2/32) Real Time Systems LAB. Agenda JNI Types and Functions –Accessing Java Strings in Native methods –Accessing Java Arrays in Native methods –Calling Java Methods Conclusion

JNI: Java Native Interface(3/32) Real Time Systems LAB. JNI Types and Functions Mapping between JNI and Native Types –References Types –Primitive Types and Native equivalents –Encoding for Java Type signatures Handling Functions –JNI String –JNI Array –JNI Method –JNI Member Variable –JNI Exception –JNI Local and Global Reference JNI Thread Synchronization Functions

JNI: Java Native Interface(4/32) Real Time Systems LAB. Mapping between JNI and Native Types(1) JNI References Types

JNI: Java Native Interface(5/32) Real Time Systems LAB. Mapping between JNI and Native Types(2)

JNI: Java Native Interface(6/32) Real Time Systems LAB. Mapping between JNI and Native Types(3)

JNI: Java Native Interface(7/32) Real Time Systems LAB. Accessing Java Strings in Native methods The JNI supports the conversion to...native Unicode and UTF-8 strings If your code tries to print a jstring directly, it'll result in a VM crash!!! JNI functions for Accessing Java Strings –GetStringUTFChars converts unicode representation of a java string into a UTF-8 string, then can directly pass the string to regular C language functions, such as “printf” In Java, UTF-8 strings are always 0 terminated –ReleaseStringUTFChars When finish using the UTF-8 string, must call this function Then VM can free the memory taken by the UTF-8 string –NewString Constructs a new java.lang.String object from an array of Unicode characters –GetStringUTFLength Returns the length of a string, represented in the UTF-8 format

JNI: Java Native Interface(8/32) Real Time Systems LAB. Declaring Native Methods Mapping Between Java and Native types

JNI: Java Native Interface(9/32) Real Time Systems LAB. Using the JNIEnv Interface Pointer In native method, using the ‘env’ pointer to reference the JNI function Access and manipulate Java objects, such as strings, throuth the env interface pointer

JNI: Java Native Interface(10/32) Real Time Systems LAB. JNI String Handling Functions GetStringChars : the Unicode GetStringUTFChars : the UTF-8 format GetStringLength GetStringUTFLength NewString NewStringUTF ReleaseStringChars ReleaseStringUTFChars

JNI: Java Native Interface(11/32) Real Time Systems LAB. Example in Java side

JNI: Java Native Interface(12/32) Real Time Systems LAB. Example in Native code side

JNI: Java Native Interface(13/32) Real Time Systems LAB. Example in JNI header file Prompt.h

JNI: Java Native Interface(14/32) Real Time Systems LAB. Accessing Java Arrays in Native methods(1) The JNI uses the jarray type to represent references to Java arrays Similar to jstring, can’t directly access jarray types in native method C code JNI functions provide that to allow obtain pointers to the individual elements of (integer…) arrays –To get the length of the array (GetArrayLength) –To obtain a pointer a pointer to the individual elements of the array, then, can retrieve the elements (GetIntArrayElements) –To release the array memory (ReleaseIntArrayElements)

JNI: Java Native Interface(15/32) Real Time Systems LAB. Accessing Java Arrays in Native methods(2) Get Length –To get the length of the array –Carry length information of java arrays Get ArrayElements –To obtain a pointer to the elements of the array –Then, can retrieve the elements –Once, obtain the pointer, can use normal C language operations Release ArrayElements –To release array memory Get ArrayRegion & Set ArrayRegion –To accessing, via copying, a Small set of elements in an array GetObjectArrayElement & SetObjectArrayElement –To access elements of object arrays –To get & set individual object array elements –Can’t get all the object array elements at once!!

JNI: Java Native Interface(16/32) Real Time Systems LAB. JNI Array Handling Functions GetArrayLength Get ArrayElements –GetBooleanArrayElements –GetByteArrayElements –GetCharArrayElements –GetDoubleArrayElements –GetFloatArrayElements –GetIntArrayElements –GetLongArrayElements –GetShortArrayElements Release ArrayElements Get ArrayRegion Set ArrayRegion GetObjectArrayElement SetObjectArrayElement

JNI: Java Native Interface(17/32) Real Time Systems LAB. Example in Java side

JNI: Java Native Interface(18/32) Real Time Systems LAB. Example in Native code side

JNI: Java Native Interface(19/32) Real Time Systems LAB. Calling Java Methods Shows calling native language functions from a Java applications Shows how a legacy C program can use the JNI to link with Java libraries, call Java methods, use Java classes, etc

JNI: Java Native Interface(20/32) Real Time Systems LAB. Calling a Java Method from Native Code The JNI supports a complete set of “callback” operations that allow you to invoke a Java method from the Native code –Locate the method using method name & method signature –Can also invoke both class & instance methods –Use the ‘javap’ tool to generate JNI style method signatures from class files

JNI: Java Native Interface(21/32) Real Time Systems LAB. Instance method invocation Call an instance method by three steps 1.JNI function “GetObjectClass” Returns the Java class object that’s the type of the Java object 2.JNI function “GetMethodID” Performs a lookup for the Java method in a given class The lookup is based on the name of the method as well as the method signature If the method doesn’t exit, ‘GetMethodID’ returns ‘0’, immediately return from the native method at that point causes a ‘NoSuchMethodError’ to be thrown in the Java application code 3.JNI function “CallVoidMethod” Invokes an instance method that has ‘void’ return type, pass the ‘object’, ‘methodID’, & the ‘actual arguments’ to CallVoidMethod

JNI: Java Native Interface(22/32) Real Time Systems LAB. Forming the Method Name and Method Signature(1) The JNI performs a symbolic lookup based on the methods’s name & type signature Method type signature –“(argument-types)return-type”

JNI: Java Native Interface(23/32) Real Time Systems LAB. Forming the Method Name and Method Signature(2) Using ‘javap’ tool to Generate Method Signatures –To print out member variables and method signatures for specified classes –‘javap –s –p Prompt’ –‘-s’ flag : to generate the member variable signatures from class files –‘-p’ flag : to include private members –The method name is the Java method name in UTF-8 form –Specify the constructor of a class as “ ”

JNI: Java Native Interface(24/32) Real Time Systems LAB. JNI Method Handling Functions GetObjectClass GetMethodID GetStaticMethodID Call Method –CallBooleanMethod –CallByteMethod –CallCharMethod –CallDoubleMethod –CallFloatMethod –CallIntMethod –CallLongMethod –CallObjectMethod –CallShortMethod –CallVoidMethod CallStatic Method CallNonvirtual Method Call MethodV Call MethodA

JNI: Java Native Interface(25/32) Real Time Systems LAB. Example in Java side Accessing Java Member Variables

JNI: Java Native Interface(26/32) Real Time Systems LAB. Example in Native code side(1) To ‘get’ & ‘set’ Java member variables from native language method

JNI: Java Native Interface(27/32) Real Time Systems LAB. Example in Native code side(1) To ‘get’ & ‘set’ Java member variables from native language method

JNI: Java Native Interface(28/32) Real Time Systems LAB. JNI Member Variable Handling Functions GetFieldID GetStaticFieldID Get Field –GetBooleanField –GetByteField –GetCharField –GetDoubleField –GetFloatField –GetIntField –GetLongField –GetObjectField –GetShortField Set Field GetStatic Field SetStatic Field

JNI: Java Native Interface(29/32) Real Time Systems LAB. JNI Exception Handling Functions ExceptionClear ExceptionDescribe ExceptionOccurred

JNI: Java Native Interface(30/32) Real Time Systems LAB. JNI Local and Global Reference Handling Functions NewGlobalRef DeleteGlobalRef DeleteLocalRef

JNI: Java Native Interface(31/32) Real Time Systems LAB. JNI Thread Synchronization Functions MonitorEnter MonitorExit

JNI: Java Native Interface(32/32) Real Time Systems LAB. Conclusion Consideration of JDK upper version’s JNI improvements JNI Thread Synchronization Invoking the JVM into a native application