Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Java method in a C++ application

Tags:

java

c++

c

Just a simple question: Is it possible to call a java function from c/c++ ?

like image 501
Hemant Avatar asked Jun 14 '09 13:06

Hemant


People also ask

How can I call a Java program from C program?

To call a specific Java function from C, you need to do the following: Obtain the class reference using the FindClass(,,) method. Obtain the method IDs of the functions of the class that you want to call using the GetStaticMethodID and GetMethodID function calls.

Can you mix C and Java?

Calling a C/C++ function from Java is actually quite easy but requires several steps: The native method must be declared in your Java code. The Java Native Interface (JNI) glue layer needs to be implemented.

How can I call C function in Java using native keyword?

Use javah -jni to generate a C header file ( HelloWorld. h ) containing the function prototype for the native method implementation. The javah tool is provided with JDK or Java 2 SDK releases. Write the C implementation ( HelloWorld.

How do you call a method in JNI?

Call a class method by following these steps: Obtain the method ID using the JNI function GetStaticMethodID rather than the function GetMethodID . Pass the class, method ID, and arguments to the family of class method invocation functions: CallStaticVoidMethod , CallStaticBooleanMethod , and so on.


2 Answers

Yes you can, but it is a little convoluted, and works in a reflective/non type safe way (example uses the C++ api which is a little cleaner than the C version). In this case it creates an instance of the Java VM from within the C code. If your native code is first being called from Java then there is no need to construct a VM instance

#include<jni.h> #include<stdio.h>  int main(int argc, char** argv) {      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");      // First get the class that contains the method you need to call     jclass clazz = env->FindClass("java/lang/String");      // Get the method that you want to call     jmethodID to_lower = env->GetMethodID(clazz, "toLowerCase",                                       "()Ljava/lang/String;");     // Call the method on the object     jobject result = env->CallObjectMethod(jstr, to_lower);      // Get a C-style string     const char* str = env->GetStringUTFChars((jstring) result, NULL);      printf("%s\n", str);      // Clean up     env->ReleaseStringUTFChars(jstr, str);      // Shutdown the VM.     vm->DestroyJavaVM(); } 

To compile (on Ubuntu):

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 

Note: that the return code from each of these methods should be checked in order to implement correct error handling (I've ignored this for convenience). E.g.

str = env->GetStringUTFChars(jstr, NULL); if (str == NULL) {     return; /* out of memory */ } 
like image 116
Michael Barker Avatar answered Oct 19 '22 18:10

Michael Barker


Yes it is, but you have to do it via JNI: http://java.sun.com/javase/6/docs/technotes/guides/jni/index.html

like image 33
CB Bailey Avatar answered Oct 19 '22 19:10

CB Bailey