



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




CMSC 212 โ S07 (lect 24)^1
CMSC 212 โ S07 (lect 24)^2
CMSC 212 โ S07 (lect 24)^3
CMSC 212 โ S07 (lect 24)^4
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
#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
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
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:
CMSC 212 โ S07 (lect 24)^10
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; }