从C code创建Java类 [英] Create JAVA class from C code
问题描述
我有类在java中名为 XMLDOMDocument
这个类有一些方法 CreateXML
, ADDNODE
,的removeNode
等我称之为从C code此方法与Java本机接口的帮助,但这样做,我传递给C $ç我的Java类$ C对象,然后调用 env-> GetObjectClass(MyClass的)
让我的类从Java类的该对象和调用方法。
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.
我想知道我可以做同样的事情(调用Java方法)没有通过我的课到C code。我可以创建用C code Java类右侧,然后调用它的方法。
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
如果我在我的C code有JavaVM的我可以创建Java类在C code的新实例与Java虚拟机的帮助。
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
我想我找到一些有用的东西创建Java虚拟机,但我想知道什么样的价值必须stetted 的#define USER_CLASSPATH
?如果一定要包的名称 com.fido.android.framework.service
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);
}
我也想知道传球类对象之间从Java到C和创建Java类权使用C区别
I also want to know the difference between passing class object from java to C and creating java class right in C
推荐答案
在CLASSPATH应定义启动Java虚拟机时,你会传递给它以同样的方式。所以,如果你想创建一个类,是com.fido.android.framework.service包里面,你就会把一些目录中生成的C可执行文件,以及一个COM /菲/安卓/框架/服务里面编译的Java类子目录。这样,你不需要更改CLASSPATH定义
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
包的名字应该在通话的findClass传递:
The package name should be passed in the FindClass call:
cls = (*env)->FindClass(env, "com/fido/android/framework/service/Prog");
这篇关于从C code创建Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!