将Java嵌入到C ++应用程序中? [英] Embed Java into a C++ application?
问题描述
我有一个用C ++编写的应用程序,我可以通过在C ++中编写插件来扩展应用程序的功能。
我基本上想做的是将Java嵌入到此应用程序中。这已经用Python完成了(不是我做的)。
我读了一些关于JNI的东西,但总是有一个完整的程序使用Java类的演讲。 / p>
我想做的是使用C ++中的类来与应用程序交互。
这是一个3D应用程序,称为Cinema 4D。
有没有办法在应用程序运行时(使用某种脚本语言)使用JNI或类似的东西来编译和评估Java代码?
嵌入完成后的假想代码示例:
import c4d .documents。*;
class Main {
public static void main(){
BaseDocument doc = GetActiveDocument();
BaseObject op = doc.GetActiveObject();
if(op!= null){
op.Remove();
}
}
}
此代码应与Cinema 4D删除所选对象。
您可以在应用程序中嵌入JVM。 Oracle的官方参考书有一些详细信息。它的概要是:
#include< jni.h> / *其中一切都被定义* /
int main(){
JavaVM * jvm; / *表示Java VM * /
JNIEnv * env; / *指向本地方法接口的指针* /
JDK1_1InitArgs vm_args; / * JDK 1.1 VM初始化参数* /
vm_args.version = 0x00010001; / * 1.1.2新增:VM版本* /
/ *获取默认初始化参数并设置类
* path * /
JNI_GetDefaultJavaVMInitArgs(& vm_args);
vm_args.classpath = ...;
/ *加载并初始化Java VM,在env * /
中返回JNI接口
*指针JNI_CreateJavaVM(& jvm,& env,& vm_args);
/ *使用JNI调用Main.test方法* /
jclass cls = env-> FindClass(Main);
jmethodID mid = env-> GetStaticMethodID(cls,test,(I)V);
env-> CallStaticVoidMethod(cls,mid,100);
/ *我们可以创建一个对象,并调用它的方法* /
/ *我们完成了。 * /
jvm-> DestroyJavaVM();
}
你想要的(例如自定义类加载器),但这是关于它在您的应用程序中获得JVM工作所需的最低需求。
I got an application written in C++ and I am able to extend the applications functionality by writing plugins in C++ for it.
What I basically want to do is to embed Java into this application. This has already been done with Python (not done by me).
I read something about JNI but there is always the speech from a full programm that uses Java classes.
What I'd like to do is, to use classes from C++ in Java to interact with the application.
It's a 3D app in this case, called Cinema 4D.
Is there a way to compile and evaluate Java code while the application is running (in some sort of scripting language) using JNI or anything like it ?
Example imaginary code after the embedding was done:
import c4d.documents.*;
class Main {
public static void main() {
BaseDocument doc = GetActiveDocument();
BaseObject op = doc.GetActiveObject();
if (op != null) {
op.Remove();
}
}
}
This code should interact with Cinema 4D to delete the selected object.
You can embed a JVM within your application. Oracle's official reference book has some more details. The synopsis of it is:
#include <jni.h> /* where everything is defined */ int main() { JavaVM *jvm; /* denotes a Java VM */ JNIEnv *env; /* pointer to native method interface */ JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */ vm_args.version = 0x00010001; /* New in 1.1.2: VM version */ /* Get the default initialization arguments and set the class * path */ JNI_GetDefaultJavaVMInitArgs(&vm_args); vm_args.classpath = ...; /* load and initialize a Java VM, return a JNI interface * pointer in env */ JNI_CreateJavaVM(&jvm, &env, &vm_args); /* invoke the Main.test method using the JNI */ jclass cls = env->FindClass("Main"); jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V"); env->CallStaticVoidMethod(cls, mid, 100); /* We could have created an Object and called methods on it instead */ /* We are done. */ jvm->DestroyJavaVM(); }
You can do far more sophisticated things if you want (e.g. custom class loaders) but that's about it in terms of the bare minimum needed to get a JVM working within your application.
这篇关于将Java嵌入到C ++应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!