Java Native Interface (JNI) - Lecture 24 Notes for CMSC 212, Study notes of Computer Science

Lecture notes for cmsc 212 - computer science course, specifically for lecture 24 on java native interface (jni). The notes cover goals, keys ideas, jni definition, process of compiling using jni, using jni functions, accessing java data from c, accessing java arrays, and calling java methods from c.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-1gj
koofers-user-1gj ๐Ÿ‡บ๐Ÿ‡ธ

5

(1)

8 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
1
CMSC 212 โ€“ S07 (lect 24)
Announcements
๎˜
Program #5
โ€“ is due today
๎˜
Program #6
โ€“ Is on the web
๎˜
Reading
โ€“ Today โ€“ Notes
โ€“ Tuesday - Notes
2
CMSC 212 โ€“ S07 (lect 24)
Java Native Interface
๎˜
Goals
โ€“ Sometimes need to access things in lower level language
โ€“ Provide full access to Java Objects in other languages
๎˜
Keys Ideas
โ€“ Need to identify signatures of native methods
โ€“ Need to incorporate native code into program
pf3
pf4
pf5

Partial preview of the text

Download Java Native Interface (JNI) - Lecture 24 Notes for CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 โ€“ S07 (lect 24)^1

Announcements

 Program

  • is due today

 Program

  • Is on the web

 Reading

  • Today โ€“ Notes
  • Tuesday - Notes

CMSC 212 โ€“ S07 (lect 24)^2

Java Native Interface

 Goals

  • Sometimes need to access things in lower level language
  • Provide full access to Java Objects in other languages

 Keys Ideas

  • Need to identify signatures of native methods
  • Need to incorporate native code into program

CMSC 212 โ€“ S07 (lect 24)^3

JNI definition

 The JNI defines a standard naming and calling

convention so the Java virtual machine can locate

and invoke native methods.

 JNI is built into the Java virtual machine so the Java

virtual machine can invoke local system calls to

perform input and output, graphics, networking, and

threading operations on the host operating system.

 Call code written in any programming language from

a program written in the Java language

  • by declaring a native Java method
  • loading the library that contains the native code
  • and then calling the native method

CMSC 212 โ€“ S07 (lect 24)^4

Process of Compiling Using JNI

Helloworld.java Java Compiler (javac)

gcc

Helloworld.class

Helloworld.h

HelloworldC.c

Javah โ€“jni

stdio.h

jni.h

Hello.so

CMSC 212 โ€“ S07 (lect 24)^7

C Code for JNI Call

#include <jni.h> #include "HelloWorld.h" #include <stdio.h>

JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf("Hello world!\n"); return; }

CMSC 212 โ€“ S07 (lect 24)^8

Accessing Java Data from C

 Mapping Types

Long Jlong 64

Int Jint 32

Short Jshort 16

Char Jchar 16

Boolean Jboolean 8, unsigned

Byte Jbyte 8

Java Type Native Type Size (bits)

CMSC 212 โ€“ S07 (lect 24)^9

Java Strings

 The String object in the Java language, which is represented as jstring in Java Native Interface (JNI), is a 16 bit unicode string.  In C a string is by default constructed from 8 bit characters.

 To access a Java language String object passed to a C or C++ function or return a C or C++ string to a Java language method, you need to use JNI conversion functions in your native method implementation.  The following converts the java string to an array of C characters:

  • (*env)->GetStringUTFChars(env, name, iscopy)  The following C JNI function converts an array of C characters to a jstring:
  • (*env)->NewStringUTF(env, lastfile)

CMSC 212 โ€“ S07 (lect 24)^10

Access Java Arrays

 Need to call special functions to get data and size  Example: JNIEXPORT jfloat JNICALL Java_FloatArray_sumArray(JNIEnv *env, jobject obj, jfloatArray arr) { jfloat *body, sum = 0; jsize I, len;

len = (env)->GetArrayLength(env, arr); body = (env)->GetFloatArrayElements(env, arr, 0); for (i=0; i<len; i++) { sum += body[i]; } (*env)->ReleaseFloatArrayElements(env, arr, body, 0); return sum; }