[Java] How to Build JNI Library on Mac OS?

[Java] How to Build JNI Library on Mac OS?

First, you need to know what’s JNI?

The Java Native Interface (JNI)is a native programming interface that lets Java code running in a JVM interoperate with native libraries written in other languages (e.g., C, C++, or even assembly language). The JNI is a bridge between the JVM and the platform.

How to Bundle Native Libraries?
■ Native code is integrated into Java as a special type of method implementation.
A Java class can declare a method as having a native implementation using the native method modifier. For example:

static native int joyGetNumDevs();

■ Classes with native code are compiled normally. But after compilation, the javah command is used to generate C header and stub files, which are used to create the native method implementations.

javah Joystick

javah analyzes Joystick.java and generates a file named Joystick.h.

■ The native code is compiled into a library in a platform-specific way for its target operating system.
■ The original Java class with the native method includes code to invoke System.load Library(), typically in a static initializer, to load the native library when the class is loaded in the Java VM.

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

■ Other classes can invoke the native method as if it were a normal method, and the Java platform takes care of the native invocation details.

A simple Java example:

class Joystick {
    static boolean init() {
        try {
            System.loadLibrary("joystick");
            return true;
        } catch (UnsatisfiedLinkError ule) {
            return false;
        }
    }

    public static void main(String[] args){
        if (!Joystick.init()){
            System.err.println("unable to load joystick library");
            return;
        }else{
            Joystick.joyGetNumDevs();
        }
    }

    static native int joyGetNumDevs();
}

After we compile this Joystick.java to Joystick.class, use javah tool to generate the header file with a function prototype for the joyGetNumDevs method automatically:

$ javac Joystick.java
$ javah Joystick

Then joystick.h will contain following code in the same folder:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class Joystick */
#ifndef _Included_Joystick
#define _Included_Joystick
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class: Joystick
 * Method: joyGetNumDevs
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_Joystick_joyGetNumDevs
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

Building the Native Interface Library
1. Writing a simple C code:

#include 
#include "Joystick.h"
JNIEXPORT jint JNICALL Java_Joystick_joyGetNumDevs(JNIEnv *pEnv, jclass clazz) {
    printf("Hello Lawrence!! \n");
}

Then save it with your java class file in the same folder.

2. Execute the following command to compile joystick.c into joystick.o:

$ gcc -g -I/System/Library/Frameworks/JavaVM.framework/Headers -c -o joystick.o joystick.c

3. Execute the following command to create native library:

$ gcc -dynamiclib -o libjoystick.jnilib joystick.o -framework JavaVM

4.Excute the Java file:

$ java Joystick

When the program is run, the following output is displayed:
Hello Lawrence!!

(Visited 18 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail
Comments are closed.