Giter Site home page Giter Site logo

Comments (1)

this-kirke avatar this-kirke commented on May 13, 2024 3

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)

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.