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 Qt8b7c:f320:99b9:690f:4595:cd17:293a:c069Core)


 qmake:
 QT += core


 Since:
 Qt 6.1

  List of all members, including inherited members Deprecated members  Public Types
  
 enum class 

OutputMode { Silent, Verbose }
  Public Functions
  
 

QJniEnvironment()
 
 

~QJniEnvironment()
 
 bool 

checkAndClearExceptions(QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069OutputMode outputMode = OutputMo8b7c:f320:99b9:690f:4595:cd17:293a:c069Verbose)
 
 jclass 

findClass(const char *className)
 
 jfieldID 

findField(jclass clazz, const char *fieldName, const char *signature)
 
 jmethodID 

findMethod(jclass clazz, const char *methodName, const char *signature)
 
 jfieldID 

findStaticField(jclass clazz, const char *fieldName, const char *signature)
 
 jmethodID 

findStaticMethod(jclass clazz, const char *methodName, const char *signature)
 
 bool 

isValid() const
 
 JNIEnv *

jniEnv() const
 
 bool 

registerNativeMethods(const char *className, const JNINativeMethod [] methods, int size)
 
 bool 

registerNativeMethods(jclass clazz, const JNINativeMethod [] methods, int size)
 
 JNIEnv &

operator*() const
 
 JNIEnv *

operator->() const
  Static Public Members
  
 bool 

checkAndClearExceptions(JNIEnv *env, QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069OutputMode outputMode = OutputMo8b7c:f320:99b9:690f:4595:cd17:293a:c069Verbose)
 
 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. For more information about JNIEnv, see Java: Interface Function Table.  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 QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069OutputMode
 

Constant
Value
Description
 
QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069OutputMo8b7c:f320:99b9:690f:4595:cd17:293a:c069Silent
0
The exceptions are cleaned silently
 
QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069OutputMo8b7c:f320:99b9:690f:4595:cd17:293a:c069Verbose
1
Prints the exceptions and their stack backtrace as an error to stderr stream.
  Member Function Documentation QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069QJniEnvironment()
 Constructs a new JNI Environment object and attaches the current thread to the Java VM. QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069~QJniEnvironment()
 Detaches the current thread from the Java VM and destroys the QJniEnvironment object. This will clear any pending exception by calling checkAndClearExceptions(). 
bool QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069heckAndClearExceptions(QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069OutputMode outputMode = OutputMo8b7c:f320:99b9:690f:4595:cd17:293a:c069Verbose)
 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 QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069heckAndClearExceptions(JNIEnv *env, QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069OutputMode outputMode = OutputMo8b7c:f320:99b9:690f:4595:cd17:293a:c069Verbose)
 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 QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069indClass(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 className is not found. A use case for this function is searching for a class to call a JNI method that takes a jclass. This can be useful when doing multiple JNI calls on the same class object which can a bit faster than using a class name in each call. Additionally, this call looks for internally cached classes first before doing a JNI call, and returns such a class if found. 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 javaMessage = QJniObject8b7c:f320:99b9:690f:4595:cd17:293a:c069romString("findClass example");
QJniObject8b7c:f320:99b9:690f:4595:cd17:293a:c069llStaticMethod<void>(javaClass, "printFromJava",
                                   "(Ljava/lang/String;)V", javaMessage.object<jstring>());  Note: This call returns a global reference to the class object from the internally cached classes.  
[since 6.2] jfieldID QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069indField(jclass clazz, const char *fieldName, const char *signature)
 Searches for an member field of a class clazz. The field is specified by its fieldName and signature. Returns the field ID or nullptr if the field is not found. A usecase for this method is searching for class fields and caching their IDs, so that they could later be used for getting/setting the fields. This function was introduced in Qt 6.2. 
[since 6.2] jmethodID QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069indMethod(jclass clazz, const char *methodName, const char *signature)
 Searches for an instance method of a class clazz. The method is specified by its methodName and signature. Returns the method ID or nullptr if the method is not found. A usecase for this method is searching for class methods and caching their IDs, so that they could later be used for calling the methods. This function was introduced in Qt 6.2. 
[since 6.2] jfieldID QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069indStaticField(jclass clazz, const char *fieldName, const char *signature)
 Searches for a static field of a class clazz. The field is specified by its fieldName and signature. Returns the field ID or nullptr if the field is not found. A usecase for this method is searching for class fields and caching their IDs, so that they could later be used for getting/setting the fields. This function was introduced in Qt 6.2. 
[since 6.2] jmethodID QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069indStaticMethod(jclass clazz, const char *methodName, const char *signature)
 Searches for a static method of a class clazz. The method is specified by its methodName and signature. Returns the method ID or nullptr if the method is not found. A usecase for this method is searching for class methods and caching their IDs, so that they could later be used for calling the methods. QJniEnvironment env;
jclass javaClass = env.findClass("org/qtproject/example/android/CustomClass");
jmethodID methodId = env.findStaticMethod(javaClass,
                                          "staticJavaMethod",
                                          "(Ljava/lang/String;)V");
QJniObject javaMessage = QJniObject8b7c:f320:99b9:690f:4595:cd17:293a:c069romString("findStaticMethod example");
QJniObject8b7c:f320:99b9:690f:4595:cd17:293a:c069llStaticMethod<void>(javaClass,
                                   methodId,
                                   javaMessage.object<jstring>()); This function was introduced in Qt 6.2. 
[since 6.2] bool QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069isValid() const
 Returns true if this instance holds a valid JNIEnv object. This function was introduced in Qt 6.2. 
[static] JavaVM *QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069javaVM()
 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 *QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069jniEnv() const
 Returns the JNI Environment's JNIEnv pointer. 
bool QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069registerNativeMethods(const char *className, const 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  const 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); 
bool QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069registerNativeMethods(jclass clazz, const JNINativeMethod [] methods, int size)
 This is an overloaded function. This overload uses a previously cached jclass instance clazz. JNINativeMethod methods[] {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
                           {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};
QJniEnvironment env;
jclass clazz = env.findClass("org/qtproject/android/TestJavaClass");
env.registerNativeMethods(clazz, methods, 2); 
JNIEnv &QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069operator*() const
 Returns the JNI Environment's JNIEnv object. 
JNIEnv *QJniEnvironment8b7c:f320:99b9:690f:4595:cd17:293a:c069operator->() const
 Provides access to the JNI Environment's JNIEnv pointer.

    
{'path': 'public/docs/qt/qjnienvironment.html', 'source': 'DevDocs.IO'}