Create JAVA class from C code

心不动则不痛 提交于 2019-12-11 00:57:36

问题


I have Class in java called XMLDOMDocument this class have some methods CreateXML, AddNode, RemoveNode etc. I call this methods from C code with the help of Java Native Interface, but for doing that I pass to the C code object of my JAVA class then by calling env->GetObjectClass(myclass) I get my class from that object and call methods of the JAVA class.

I want to know can I do same thing (call JAVA methods) without passing my class to the C code. Can I create JAVA class right in C code and then call it's methods.

Edited

And if I have JavaVM in my C code can I create a new instance of JAVA class in C code with the help of that Java VM.

Edited

I think I found something useful Creating the Java Virtual Machine, but I want to understand what value must be stetted #define USER_CLASSPATH ? If that must be package name com.fido.android.framework.service

public class Prog {
     public static void main(String[] args) {
          System.out.println("Hello World " + args[0]);
     }
 }

#include <jni.h>

 #define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
 #define USER_CLASSPATH "." /* where Prog.class is */

 main() {
     JNIEnv *env;
     JavaVM *jvm;
     jint res;
     jclass cls;
     jmethodID mid;
     jstring jstr;
     jclass stringClass;
     jobjectArray args;

 #ifdef JNI_VERSION_1_2
     JavaVMInitArgs vm_args;
     JavaVMOption options[1];
     options[0].optionString =
         "-Djava.class.path=" USER_CLASSPATH;
     vm_args.version = 0x00010002;
     vm_args.options = options;
     vm_args.nOptions = 1;
     vm_args.ignoreUnrecognized = JNI_TRUE;
     /* Create the Java VM */
     res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
 #else
     JDK1_1InitArgs vm_args;
     char classpath[1024];
     vm_args.version = 0x00010001;
     JNI_GetDefaultJavaVMInitArgs(&vm_args);
     /* Append USER_CLASSPATH to the default system class path */
     sprintf(classpath, "%s%c%s",
             vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
     vm_args.classpath = classpath;
     /* Create the Java VM */
     res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
 #endif /* JNI_VERSION_1_2 */

     if (res < 0) {
         fprintf(stderr, "Can't create Java VM\n");
         exit(1);
     }
     cls = (*env)->FindClass(env, "Prog");
     if (cls == NULL) {
         goto destroy;
     }

     mid = (*env)->GetStaticMethodID(env, cls, "main",
                                     "([Ljava/lang/String;)V");
     if (mid == NULL) {
         goto destroy;
     }
     jstr = (*env)->NewStringUTF(env, " from C!");
     if (jstr == NULL) {
         goto destroy;
     }
     stringClass = (*env)->FindClass(env, "java/lang/String");
     args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
     if (args == NULL) {
         goto destroy;
     }
     (*env)->CallStaticVoidMethod(env, cls, mid, args);

 destroy:
     if ((*env)->ExceptionOccurred(env)) {
         (*env)->ExceptionDescribe(env);
     }
     (*jvm)->DestroyJavaVM(jvm);
 }

I also want to know the difference between passing class object from java to C and creating java class right in C


回答1:


The CLASSPATH should be defined the same way you would pass it when launching the Java Virtual Machine. So if you want to create a class that is inside the com.fido.android.framework.service package, you would put the generated C executable in some directory, and the compiled Java class inside a com/fido/android/framework/service sub-directory. That way you do not need to change the CLASSPATH define

The package name should be passed in the FindClass call:

cls = (*env)->FindClass(env, "com/fido/android/framework/service/Prog");


来源:https://stackoverflow.com/questions/10877529/create-java-class-from-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!