https://github.com/nokia/cppjni
A C++-Java communication framework, which aim to simplify usage of Java code in C++ code.
https://github.com/nokia/cppjni
Last synced: about 1 year ago
JSON representation
A C++-Java communication framework, which aim to simplify usage of Java code in C++ code.
- Host: GitHub
- URL: https://github.com/nokia/cppjni
- Owner: nokia
- License: bsd-3-clause
- Created: 2017-10-12T10:22:34.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-08-03T15:10:13.000Z (almost 6 years ago)
- Last Synced: 2025-06-11T06:04:24.274Z (about 1 year ago)
- Language: C++
- Size: 101 KB
- Stars: 15
- Watchers: 4
- Forks: 6
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
cppJNI
=======
Java Native Interface (JNI) framework wrapper, which uses new C++ features in order to add type safety.
Requirements
--------
* Boost 1.61 or higher (http://www.boost.org)
* C++14 compatible compiler (clang >= 3.8, gcc >= 6.0)
* google test for UT (>= 1.8)
Quick Start
--------
With below silly Java class:
```Java
package jni.test;
public class JNITest {
private int privatePrimitiveField;
private String privateObjectField;
public JNITest(int primitiveParam, String objectParam) {
this.privatePrimitiveField = primitiveParam;
this.privateObjectField = objectParam;
}
public int getPrimitiveField() {
return privatePrimitiveField;
}
public String getStringField() {
return privateObjectField;
}
}
```
Get back to C++ and prepare your Java class description:
```C++
#include
//class description is a template that must derive it's sole template parameter
template
struct MyJNIClass: T
{
//path to your class package
using class_path_t = typestring_is("jni/test/MyJNIClass");
//here you create constructor signature: it returns object of your class, obviously, and takes one integer and string in turn
MAKE_JAVA_CONSTRUCTORS(jni::types::Object<::MyJNIClass>(jni::types::Int, jni::types::String))
//similarly you can declare methods
MAKE_JAVA_METHOD(getPrimitiveField, jni::types::Method)
MAKE_JAVA_METHOD(getStringField, jni::types::Method)
};
```
Create JVM:
```C++
#include
...
jni::VirtualMachine vm({"-Djava.class.path=path-to-your-jar-file"});
```
Create some classes and work with them:
```C++
...
auto myClass = vm.loadClass();
auto javaObject = myClass.Construct(5, "123ads");
cout << javaObject.getPrimitiveField() << " '" << javaObject.getStringField() << "'" << endl;
...
```
It can't be simpler!
TODO:
--------
* registering of native methods
* Java monitors support
* arrays support
* class field access