Evaluating the Java Native Interface (JNI): Leveraging Existing Native Code, Libraries and Threads to a Running Java Virtual Machine

Evaluating the Java Native Interface (JNI): Leveraging Existing Native Code, Libraries and Threads to a Running Java Virtual Machine

Stelios Sotiriadis, Oladotun Omosebi, Assem Ayapbergenova, Nurbek P. Saparkhojayev
Copyright: © 2018 |Pages: 23
DOI: 10.4018/IJDST.2018040104
OnDemand:
(Individual Articles)
Available
$37.50
No Current Special Offers
TOTAL SAVINGS: $37.50

Abstract

This article aims to explore JNI features and to discover fundamental operations of the Java programming language, such as arrays, objects, classes, threads and exception handling, and to illustrate these by using various algorithms and code samples. The authors also investigate the JNI Invocation API that allows native applications to interact with the Java Virtual Machine (JVM). They focus on attaching native threads to a running JVM and on leveraging existing native code using one-to-one mapping and shared stubs.
Article Preview
Top

Introduction

The Java Native Interface (JNI, 2014) aims to provide interoperability between Java code running on Java Virtual Machine (JVM) and code written in other programming languages like C, C++, assembly, python and others. It is typically used for utilizing certain platform depended features, for reusing existing libraries and for speeding up time critical execution of applications. Using the JNI, developers implement Java classes that are extended with native methods and linked dynamically. Using JNI, a Java application invokes native methods, submits parameters and obtains results after the native method has finished processing. The latter can both create and invoke operations on existing Java objects to: (a) create, update and inspect Java objects, (b) get any primitive data types or objects, (c) return either Java primitive data types or objects back to the Java program, (d) call Java instance or class methods, pass it the required parameters and get the results back when the method completes, (e) catch and throw Java exceptions and, (f) implement synchronization to support multithreaded access to a running JVMs.

Classes and Objects

JNI provides functions that allow the native code to access a field or call a method within a Java object or a Java class. Figure 1 demonstrates the classes and objects JNI methods classification.

Figure 1.

Classification of JNI Array methods

IJDST.2018040104.f01

All these functions require a value of jclass type that contains the information for the class in which the field or method is declared. JNI defines two classes namely FindClass() and GetObjectClass(), which can be used from native code to get jclass objects. Next, we outline the characteristics of each function.

  • 1.

    The FindClass() method takes as parameter the fully qualified class name with package components and returns the jclass value for the given class or a NULL value. The FindClass() browses all directories and files defined in the Java classpath environment variable for a given class. The statement - jclass strClass = env -> FindClass(“Java/lang/String”) - demonstrates a call based on the FindClass() function to obtain the jclass value for the string class, where Java/lang/String is the fully qualified name for the string class. Another case involves the statement - jclass intClass = env -> FindClass(“[I”) - where the FindClass() obtains the jclass value for an array of Java integer type, with the signature being “I”, while the JNI int[] is represented as “[I”. Finally, the FindClass() function is useful when the native code knows the fully qualified name o the class or the class signature.

  • 2.

    The GetObjectClass() function takes an object as parameter and returns the jclass value of the class from which the object was instantiated, or returns a NULL value. This function is useful when the native code requires identifying the class for an object that is passed as an argument. The statement - jclass xClass = env -> GetObjectClass(x) - represents a call using the GetObjectClass() function within a native method to obtain the class of an object passed into a native method as argument.

Complete Article List

Search this Journal:
Reset
Volume 15: 1 Issue (2024)
Volume 14: 2 Issues (2023)
Volume 13: 8 Issues (2022)
Volume 12: 4 Issues (2021)
Volume 11: 4 Issues (2020)
Volume 10: 4 Issues (2019)
Volume 9: 4 Issues (2018)
Volume 8: 4 Issues (2017)
Volume 7: 4 Issues (2016)
Volume 6: 4 Issues (2015)
Volume 5: 4 Issues (2014)
Volume 4: 4 Issues (2013)
Volume 3: 4 Issues (2012)
Volume 2: 4 Issues (2011)
Volume 1: 4 Issues (2010)
View Complete Journal Contents Listing