Friday, April 19, 2013

jni jnienv FindClass example c c++ java


FindClass


jclass FindClass(JNIEnv *env, const char *name);

In JDK release 1.1, this function loads a locally-defined class. It searches the directories and zip files specified by the CLASSPATH environment variable for the class with the specified name.
Since Java 2 SDK release 1.2, the Java security model allows non-system classes to load and call native methods. FindClass locates the class loader associated with the current native method; that is, the class loader of the class that declared the native method. If the native method belongs to a system class, no class loader will be involved. Otherwise, the proper class loader will be invoked to load and link the named class.
Since Java 2 SDK release 1.2, when FindClass is called through the Invocation Interface, there is no current native method or its associated class loader. In that case, the result of ClassLoader.getSystemClassLoader is used. This is the class loader the virtual machine creates for applications, and is able to locate classes listed in the java.class.path property.
The name argument is a fully-qualified class name or an array type signature . For example, the fully-qualified class name for the java.lang.String class is:
                   "java/lang/String"

The array type signature of the array class java.lang.Object[] is:
                   "[Ljava/lang/Object;"
LINKAGE:
Index 6 in the JNIEnv interface function table.
PARAMETERS:

env: the JNI interface pointer.

name: a fully-qualified class name (that is, a package name, delimited by “/”, followed by the class name). If the name begins with “[“ (the array signature character), it returns an array class. The string is encoded in modified UTF-8.

RETURNS:

Returns a class object from a fully-qualified name, or NULL if the class cannot be found.

THROWS:

ClassFormatError: if the class data does not specify a valid class.

ClassCircularityError: if a class or interface would be its own superclass or superinterface.

NoClassDefFoundError: if no definition for a requested class or interface can be found.

OutOfMemoryError: if the system runs out of memory.

Example - FindClass

// ====================================================
// Java_SysInfo_getVersion
//
// Get the native operating system's version.
//
// Arguments:
//
// env     - pointer to pointer to JNI function table
// clazz   - handle of class that contains this method
//
// Return:
//
// Version object containing native version information
// ====================================================
JNIEXPORT jobject JNICALL Java_SysInfo_getVersion (JNIEnv *env, jclass clazz)
{
    OSVERSIONINFO osVersionInfo;
    // Get operating system version information.
    osVersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
    GetVersionEx (&osVersionInfo);
    // Attempt to find the Version class.
    clazz = env->FindClass ("Version");
    // If this class does not exist then return null.
    if (clazz == 0)
            return 0;
    // Allocate memory for a new Version class object.  Do not bother calling
    // the constructor (the default constructor does nothing).
    jobject obj = env->AllocObject (clazz);
    // Attempt to find the major field.
    jfieldID fid = env->GetFieldID (clazz, "major", "I");
    // If this field does not exist then return null.
    if (fid == 0)
            return 0;
    // Set the major field to the operating system's major version.
    env->SetIntField (obj, fid, osVersionInfo.dwMajorVersion);
    // Attempt to find the minor field.
    fid = env->GetFieldID (clazz, "minor", "I");
    // If this field does not exist then return null.
    if (fid == 0)
            return 0;
    // Set the minor field to the operating system's minor version.
    env->SetIntField (obj, fid, osVersionInfo.dwMinorVersion);
    return obj;
}