Home > Backend Development > C++ > How can Java methods be invoked from C applications?

How can Java methods be invoked from C applications?

Susan Sarandon
Release: 2024-11-09 17:35:02
Original
822 people have browsed it

How can Java methods be invoked from C   applications?

Invoking Java Methods from C Applications

In the realm of cross-platform integration, the ability to seamlessly interact between different programming languages becomes paramount. One such scenario involves calling Java functions from within C applications.

While not straightforward, this feat is indeed achievable through a reflective approach. By leveraging Java's Native Interface (JNI), C can interact with the Java Virtual Machine (JVM). Here's a detailed explanation of the process:

  1. Create a Java Virtual Machine (JVM):

    • Initialize the JNI by creating a JavaVM object and corresponding JNIEnv* environment.
    • Specify the desired JNI version and options through JavaVMInitArgs.
  2. Obtain the Java Object:

    • Convert the required Java object into a C jstring using NewStringUTF.
  3. Locate the Java Method:

    • Find the class that contains the targeted method using FindClass.
    • Identify the method by name and signature using GetMethodID.
  4. Call the Java Method:

    • Invoke the Java method on the object with CallObjectMethod.
    • Retrieve the result as a jobject and cast it to the appropriate type.
  5. Extract the Result:

    • Convert the jstring result to a C-style string using GetStringUTFChars.
  6. Clean Up:

    • Release the JNI resources with ReleaseStringUTFChars.
    • Destroy the JVM with DestroyJavaVM.

Example Code:

#include <jni.h>
#include <stdio.h>

int main() {
    JavaVM *vm;
    JNIEnv *env;
    JavaVMInitArgs vm_args;
    vm_args.version = JNI_VERSION_1_2;
    vm_args.nOptions = 0;
    vm_args.ignoreUnrecognized = 1;

    // Construct a VM
    jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);

    // Construct a String
    jstring jstr = env->NewStringUTF("Hello World");

    // Find the class and method
    jclass clazz = env->FindClass("java/lang/String");
    jmethodID to_lower = env->GetMethodID(clazz, "toLowerCase", "()Ljava/lang/String;");

    // Call the method
    jobject result = env->CallObjectMethod(jstr, to_lower);

    // Get the C-style string
    const char *str = env->GetStringUTFChars((jstring)result, NULL);

    printf("%s\n", str);

    // Clean up
    env->ReleaseStringUTFChars(jstr, str);
    vm->DestroyJavaVM();
    return 0;
}
Copy after login

To compile the code on Linux, run:

g++ -I/usr/lib/jvm/java-6-sun/include -I/usr/lib/jvm/java-6-sun/include/linux -L/usr/lib/jvm/java-6-sun/jre/lib/i386/server/ -ljvm jnitest.cc
Copy after login

Note: Implement proper error handling by checking the return codes of JNI methods.

The above is the detailed content of How can Java methods be invoked from C applications?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template