Skip to content
/ jnipp Public
forked from mitchdowd/jnipp

C++ wrapper for the Java Native Interface

License

Notifications You must be signed in to change notification settings

Kian738/jnipp

This branch is 7 commits ahead of mitchdowd/jnipp:master.

Folders and files

NameName
Last commit message
Last commit date
May 17, 2024
Oct 15, 2018
Jun 5, 2024
Jun 5, 2024
Apr 19, 2024
Apr 19, 2024
Sep 26, 2021
Oct 25, 2023
Apr 19, 2024
Oct 21, 2016
Sep 26, 2021
Oct 15, 2018
Jan 22, 2025
Jan 21, 2025
Nov 2, 2016
Jun 20, 2019
Nov 2, 2016
Nov 26, 2022
Apr 19, 2024

Repository files navigation

Java Native Interface for C++

Overview

JNIPP is just a C++ wrapper for the standard Java Native Interface (JNI). It tries to take some of the long-winded annoyance out of integrating your Java and C++ code.

While this project has so far just been a utility library for my own usage, it seems to have caught the eye of some others who have also been looking for a suitable C++ JNI layer. If you have feature requests, do not hesitate to submit them as Github issues. Please be descriptive in your feature request. The more useful information you provide - along with justification on why it should be implemented - the more likely it is that I will add your feature.

Requirements

To compile you will need:

  • A C++11 compatible compiler
  • An installation of the Java Development Kit (JDK)
  • The JAVA_HOME environment variable, directed to your JDK installation.

Usage

For comprehensive examples on how to use jnipp, see the tests project in the project source code.

There are two situations where the Java Native Interface would be needed.

  • A Java application calling C/C++ functions; or
  • A C/C++ application calling Java methods

Calling Java from C++

The following is an example of calling Java from C++.

#include <jnipp.h>

int main()
{
    // An instance of the Java VM needs to be created.
    jni::Vm vm;

    // Create an instance of java.lang.Integer
    jni::Class Integer = jni::Class("java/lang/Integer");
    jni::Object i = Integer.newInstance("1000");

    // Call the `toString` method on that integer
    std::string str = i.call<std::string>("toString");

    // The Java VM is automatically destroyed when it goes out of scope.
    return 0;
}

Calling C++ from Java

Consider a basic Java program:

package com.example;

class Demo {
    public int value;

    public static void main(String[] args) {
        Demo demo = new Demo();
        demo.value = 1000;
        demo.run();
    }

    public native void run();
}

A matching C++ library which uses jnipp could look like:

#include <jnipp.h>
#include <iostream>

/*
    The signature here is defind by the JNI standard, so must be adhered to.
    Although, to prevent pollution of the global namespace, the JNIEnv and
    jobject types defind by the standard JNI have been placed into the
    jni namespace.
 */
extern "C" void Java_com_example_Demo_run(jni::JNIEnv* env, jni::jobject obj)
{
    // jnipp only needs initialising once, but it doesn't hurt to do it again.
    jni::init(env);

    // Capture the supplied object.
    jni::Object demo(obj);

    // Print the contents of the `value` field to stdout.
    std::cout << demo.get<int>("value") << std::endl;
}

Configuration

By default, jnipp uses std::runtime_error as the base exception class. If you wish, you can define JNIPP_EXCEPTION_CLASS to be the exception class you wish to use, before including jnipp.h. It just needs a const char* constructor.

About

C++ wrapper for the Java Native Interface

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 98.2%
  • CMake 1.4%
  • Makefile 0.4%