QJniEnvironment Class
The QJniEnvironment class provides access to the JNI Environment (JNIEnv). More...
Header: | #include <QJniEnvironment> |
CMake: | find_package(Qt6 COMPONENTS Core REQUIRED) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Since: | Qt 6.1 |
Public Types
enum class | OutputMode { Silent, Verbose } |
Public Functions
QJniEnvironment() | |
~QJniEnvironment() | |
bool | checkAndClearExceptions(QJniEnvironment::OutputMode outputMode = OutputMode::Verbose) |
jclass | findClass(const char *className) |
JNIEnv * | jniEnv() const |
bool | registerNativeMethods(const char *className, JNINativeMethod [] methods, int size) |
JNIEnv & | operator*() const |
JNIEnv * | operator->() const |
Static Public Members
bool | checkAndClearExceptions(JNIEnv *env, QJniEnvironment::OutputMode outputMode = OutputMode::Verbose) |
JavaVM * | javaVM() |
Detailed Description
When using JNI, the JNIEnv class is a pointer to a function table and a member function for each JNI function that indirects through the table. JNIEnv
provides most of the JNI functions. Every C++ native function receives a JNIEnv
as the first argument. The JNI environment cannot be shared between threads.
Since JNIEnv
doesn't do much error checking, such as exception checking and clearing, QJniEnvironment allows you to do that easily.
Note: This API has been designed and tested for use with Android. It has not been tested for other platforms.
Member Type Documentation
enum class QJniEnvironment::OutputMode
Constant | Value | Description |
---|---|---|
QJniEnvironment::OutputMode::Silent |
0 |
The exceptions are cleaned silently |
QJniEnvironment::OutputMode::Verbose |
1 |
Prints the exceptions and their stack backtrace as an error to stderr stream. |
Member Function Documentation
QJniEnvironment::QJniEnvironment()
Constructs a new JNI Environment object and attaches the current thread to the Java VM.
QJniEnvironment::~QJniEnvironment()
Detaches the current thread from the Java VM and destroys the QJniEnvironment object. This will clear any pending exception by calling checkAndClearExceptions().
bool QJniEnvironment::checkAndClearExceptions(QJniEnvironment::OutputMode outputMode = OutputMode::Verbose)
Cleans any pending exceptions either silently or reporting stack backtrace, depending on the outputMode.
In contrast to QJniObject, which handles exceptions internally, if you make JNI calls directly via JNIEnv
, you need to clear any potential exceptions after the call using this function. For more information about JNIEnv
calls that can throw an exception, see JNI Functions.
Returns true
when a pending exception was cleared.
[static]
bool QJniEnvironment::checkAndClearExceptions(JNIEnv *env, QJniEnvironment::OutputMode outputMode = OutputMode::Verbose)
Cleans any pending exceptions for env, either silently or reporting stack backtrace, depending on the outputMode. This is useful when you already have a JNIEnv
pointer such as in a native function implementation.
In contrast to QJniObject, which handles exceptions internally, if you make JNI calls directly via JNIEnv
, you need to clear any potential exceptions after the call using this function. For more information about JNIEnv
calls that can throw an exception, see JNI Functions.
Returns true
when a pending exception was cleared.
jclass QJniEnvironment::findClass(const char *className)
Searches for className using all available class loaders. Qt on Android uses a custom class loader to load all the .jar files and it must be used to find any classes that are created by that class loader because these classes are not visible when using the default class loader.
Returns the class pointer or null if is not found.
A use case for this function is searching for a custom class then calling its member method. The following code snippet creates an instance of the class CustomClass
and then calls the printFromJava()
method:
QJniEnvironment env; jclass javaClass = env.findClass("org/qtproject/example/android/CustomClass"); QJniObject classObject(javaClass); QJniObject javaMessage = QJniObject::fromString("findClass example"); classObject.callMethod<void>("printFromJava", "(Ljava/lang/String;)V", javaMessage.object<jstring>());
[static]
JavaVM *QJniEnvironment::javaVM()
Returns the Java VM interface for the current process. Although it might be possible to have multiple Java VMs per process, Android allows only one.
JNIEnv *QJniEnvironment::jniEnv() const
Returns the JNI Environment's JNIEnv
pointer.
bool QJniEnvironment::registerNativeMethods(const char *className, JNINativeMethod [] methods, int size)
Registers the Java methods in the array methods of size size, each of which can call native C++ functions from class className. These methods must be registered before any attempt to call them.
Returns True if the registration is successful, otherwise False.
Each element in the methods array consists of:
- The Java method name
- Method signature
- The C++ functions that will be executed
JNINativeMethod methods[] {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)}, {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}}; QJniEnvironment env; env.registerNativeMethods("org/qtproject/android/TestJavaClass", methods, 2);
JNIEnv &QJniEnvironment::operator*() const
Returns the JNI Environment's JNIEnv
object.
JNIEnv *QJniEnvironment::operator->() const
Provides access to the JNI Environment's JNIEnv
pointer.
© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.1/qjnienvironment.html