Git Product home page Git Product logo

jni-learning's Introduction

JNI-Learning

My environment:

  • OS: Mac 10.10.1
  • c compiler: Apple LLVM version 6.0

Notes

example/hellojni tutorial

This is based on the JavaNativeInterface and Java Fundamentals Tutorial : Java Native Interface Jni.

Notice the header file, listed below, has many types -- Type Conversion .

  • Primitive type matching:

    primitive type matching

  • Reference types

    JNI passes objects to native methods as opaque references, C pointer types that refer to internal data structures in the JVM.

     JNIEXPORT void JNICALL Java_com_marakana_jniexamples_Hello_sayHi
       (JNIEnv *, jobject, jstring, jint);
  • Native Method Arguments:

    • JNIEnv:

      pointer that points to another pointer pointing to a function table (array of pointer). Each entry in this function table points to a JNI function. These are the functions we are going to use for type conversion

    • jobject:

      It's the second argument for instance method, refering to the object on which the method is invoked.

    • jclass:

      It's the second argument for static method refering to the class in which the method is define.

  • String Conversion

    • (*env)->GetStringUTFChars(env, who, &iscopy);

      Convert java String to C string.

    • Other mathod can be found in JNI Functions.

Questions

Where can I find the jni headers for Mac OS

HelloJNI.o : HelloJNI.c HelloJNI.h
gcc -I"/System/Library/Frameworks/JavaVM.framework/Headers" -fpic -c $< -o $@

What kind of extension is loaded on Max?

.jnilib extension is used. Refer to Mac Developer Library.

For example, if the files hello.c and hola.c contain the implementations of the native methods to be built into a dynamic shared JNI library that will be called with System.loadLibrary(“hello”), you would build the resultant library, libhello.jnilib, with this code:

cc -c -I/System/Library/Frameworks/JavaVM.framework/Headers hola.c
cc -c -I/System/Library/Frameworks/JavaVM.framework/Headers hello.c
cc -dynamiclib -o libhello.jnilib hola.o hello.o -framework JavaVM

Often JNI libraries have interdependencies. For example assume the following:

  • libA.jnilib contains a function foo().
  • libB.jnilib needs to link against libA.jnilib to make use of foo().

Naming of jnilib

Refer to the example/hellojni :

System.loadLibrary("HelloImpl");
gcc -o libHelloImpl.jnilib -lc -shared 
	-I"/System/Library/Frameworks/JavaVM.framework/Headers" Hello.c # -> jnilib

export LD_LIBRARY_PATH=.

When we load libray AAA, LD_LIBRARY_PATH must be libAAA.

java.lang.UnsatisfiedLinkError

Common mistakes resulting in java.lang.UnsatisfiedLinkError usually come from incorrect naming of the shared library (O/S-dependent), the library not being in the search path, or wrong library being loaded by Java code.

jni-learning's People

Contributors

timmy00274672 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.