Skip to content

remram44/java-cpp-example

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

Say Thanks!

Example of using C++ code from Java

This repository contains three separate examples of calling a simple C++ library from Java code.

The example library doesn't do anything but contains a class, MyClass, forcing us to support C++ (and not just plain C).

CMake is used to build this, since locating the dependencies got outside the range of my make-fu. The project is supposed to be portable to all major platforms.

Three different wrappers are used, documented below. Each one achieves the same goal, which is using this C++ class from Java code.

SWIG

This needs the SWIG tool: http://www.swig.org/

SWIG parses the C++ header file and generates both the C++ JNI code and the Java native and helper classes.

JNA

JNA allows Java code to use C dynamic libraries directly. Because our library is in fact C++, a small wrapper is included that wraps it as C code.

You will need the JNA jar to run this: https://github.com/java-native-access/jna

JNI

This examples uses JNI directly, without auto-generation. The MyClass class in Java code just declares its methods as being "native", they are provided by the dynamic library.

About

Example of using C++ classes from Java. Showcases SWIG, JNA and JNI

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published