Git Product home page Git Product logo

jni's Introduction

JNI

Java Native Interface (JNI) example. This is a very simple example of how to call a C/C++ function from within Java.

Requirements

You either need cmake, or know how to build a shared lib. I use cmake since a shared lib is a .dll in Windows, .so in Linux and .dylib in macOS. CMake is a little more cross platform friendly.

Building and running

Linux/Mac

$ cmake CMakeLists.txt
$ make
$ javac -d . src/java/App.java
$ java -Djava.library.path=. App

Windows

TODO

How it works

There are two parts to this app. The Java app itself, and the C++ shared library that the Java app uses via the Java Native Interface.

The Java piece

You will notice 2 methods declared native in src/java/App.java. This tells Java that you will be using native code, and are expected to be in the ahred library:

public native int add(int n);
public native int print(String msg);

The C++ Piece

Two steps are involved here. First, you would normally create a header file via the following command. Note however that this was already done when I created the project:

javah -cp src/java App

Once the header file is created, you will create the C++ file, and implement the functions using the function signatures provided in the header. You will see this done in src/cpp/App.cpp.

Passing parameters to C++

In this example, I have passed an integer, and a String to C++. You can use the int's normally, but strings need to be converted. Do this will a call to

const char *str = env->GetStringUTFChars(string, 0);

str will then point to the string C++ compatible character string.

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.