Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Native Interface - Lecture Slides | CMSC 212, Study notes of Computer Science

Material Type: Notes; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-bzi-1
koofers-user-bzi-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

Related documents


Partial preview of the text

Download Java Native Interface - Lecture Slides | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 – S05 (lect 26)^1

Announcements

l Program

l Final Exam

  • Thursday, Dec 15
  • in H.J. Patterson 0226.
  • 4:00-6:00 pm

l Reading

  • http://java.sun.com/docs/books/tutorial/native1.1/

l Office Hours

  • TA office hours are through Wednesday, December 14
  • My office hours tomorrow
    • 10:00-11:
    • If PG schools are delayed or closed – cancelled
  • My office hours next week
    • Regular through Tuesday
    • I’ll post extra office hours for Wed and Thurs
    • None on Friday

CMSC 212 – S05 (lect 26)^2

Java Native Interface

l Goals (combine code from different languages in both

directions)

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

l Different Reasons for Both Directions

  • When would you want to use “native methods” from Java Code?
  • When would you want to use Java code from a native application?

l Keys Ideas

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

CMSC 212 – S05 (lect 26)^3

Process of Compiling Using JNI

Helloworld.java Java Compiler (javac)

gcc with dynamic lib flags

Helloworld.class

Helloworld.h

javah –jni

stdio.h

jni.h Hello.so

Write Java code

java HelloWorld

Hello World!!

Write C code

Helloworld.c

CMSC 212 – S05 (lect 26)^4

Using JNI Functions

l Declaring Functions in Java

  • (where the function to be used is written in C)
  • Declare like a normal method, but add “native” keyword
  • public native void displayHelloWorld();

l Loading the Code

  • Use loadLibrary call
  • Making it static forces it to happen automatically
  • System.loadLibrary("hello")
  • Java’s equivalent of dlopen
  • adds the appropriate name completion for this system

l Invoke Native Method

  • new HelloWorld().displayHelloWorld();
  • this invokes the C code

CMSC 212 – S05 (lect 26)^5

C Function Signature-Constructing the function name

  • Java_ + Full Classname + _ + Method Name JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject);

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

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

class HelloWorld { public native void displayHelloWorld();

static { System.loadLibrary("hello"); }

public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } }

CMSC 212 – S05 (lect 26)^6

Accessing Java Data from C

l 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 – S05 (lect 26)^7

Java Object Types

jobject

jstring java.lang.String objects jthrowable java.lang.Throwable exceptions

jarray (jintArray, jfloatArray…)

jclass java.lang.Class objects

CMSC 212 – S05 (lect 26)^8

Access Java Arrays

l Need to call special functions to get data and size

l 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; }

CMSC 212 – S05 (lect 26)^9

Calling Java Methods from C

l Steps

  • Get Object handle
  • Get MethodID – need to know signature
    • (argument-types)return-type
  • Call method

l Example

JNIEXPORT void JNICALL Java_Callbacks_nativeMethod( JNIEnv env, jobject obj, jint depth) { jclass cls = (env)->GetObjectClass(env, obj); jmethodID mid = (env)->GetMethodID(env, cls, "callback", "(I)V"); if (mid == 0) { return; } printf("In C, depth = %d, about to enter Java\n", depth); (env)->CallVoidMethod(env, obj, mid, depth); printf("In C, depth = %d, back from Java\n", depth); }

CMSC 212 – S05 (lect 26)^10

Accessing Object Fields

l Getting FieldID

  • Need to get a unique id for the field
  • Must specify type information
  • fid = (*env)->GetFieldID(env, cls, "s", "Ljava/lang/String;");
    • cls is of type jclass
    • s is field name
  • Can make one call to GetFieldID
    • Really computes offset for item

l Getting the data

  • Pass the fid, env, and object
  • str = (*env)->GetObjectField(env, obj, fid);