Comments (1)
Not a maintainer or contributor, but I've been working with this library a bit lately. Here's a simple gist which shows how to register a static native method to a java class method. For strings, the methods you want to know are:
const char* c_str = "Hello World";
std::string std_str( c_str );
jni::Local<jni::String> c_string_to_java_string = jni::Make<jni::String>( env, c_str );
jni::Local<jni::String> std_string_to_java_string = jni::Make<jni::String>( env, std_str );
std::string std_str_from_java_string = jni::Make<std::string>( env, c_string_to_java_string );
The high-level API is all about the tags, which are defined in the public const char* method Name(). All that makes jni::String special is literally this:
struct StringTag
{
static constexpr auto Name() { return "java/lang/String"; }
};
In the high level API, jni::Class, jni::Object, and jni::Field are generally passed by reference, and are wrapped in Local<> or Global<> ownership types which both derive from jni::Unique. They cannot be copy-constructed, and need to be constructed via one of the static methods (jni::Class::Find( env ) for example) or with jni::NewLocal or jni::NewGlobal. The deleted constructor error occurs when you try to directly create jni::Object, while you should be creating a reference to that object.
Start with the Readme, then look at examples/native_peer.cpp. This should help you get started. The NativePeer example is an invaluable use case, it solves and neatly hides pointer to member function registration. Finally, I found the most help in test/high_level.cpp - this lays out all the syntax you need for almost everything.
from jni.hpp.
Related Issues (20)
- Is there a way to add more method to native peer HOT 2
- jni::Object<> doesn't automatically cast from derived to base class HOT 1
- Some warnings on g++ HOT 2
- Fix compilation with -Wdouble-promotion
- Should high-level bindings always seize local refs?
- High-level bindings should have more implicit derived-to-base conversions
- Cast's arguments are in the wrong order HOT 1
- non-throwing New{Local,Global}Ref for weak refs HOT 1
- Add java.lang.ref.WeakReference wrapper HOT 1
- Native method signatures HOT 1
- Incorrect wrapping code HOT 1
- How can I use the library to expose a native method taking std::string? HOT 1
- How do I convert a jobject to jni::Object? HOT 2
- Null value
- Migrate to CMake.
- Questions about returning objects and using function objects
- Static methods registration HOT 1
- [BUG] jni::Class/Object::Call() blindly passes arguments to JNI's variable length argument methods
- A C++ example containing a main() function that calls a function inside a running java application would be really appreciated.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jni.hpp.