C ++中的模块路径到Java JNI调用

时间:2018-04-11 17:19:35

标签: java java-native-interface java-9 java-module jnienv

当我在C ++中创建Java 8 JVM时,我通常使用类似下面的代码来告诉JVM类路径:

JavaVMOption* options = new JavaVMOption[1];   // JVM invocation options
options[0].optionString = (char *)"-Djava.class.path=.;./lib2";   // where to find java .class
vm_args.version = JNI_VERSION_1.8;             // minimum Java version
vm_args.nOptions = 1;                          // number of options
vm_args.options = options;
vm_args.ignoreUnrecognized = false;

但是如何告诉Java 9 JVM模块路径?没有java.module.path系统属性。我能找到的最好的东西是:

JavaVMOption* options = new JavaVMOption[2];   // JVM invocation options
options[0].optionString = (char *)"-Djdk.module.path=.;./lib2";   // where to find java .class
options[1].optionString = (char *)"-Djdk.module.main=RemkaAgentService";   // where to find java .class
vm_args.version = JNI_VERSION_9;             // minimum Java version
vm_args.nOptions = 2;                          // number of options
vm_args.options = options;
vm_args.ignoreUnrecognized = false;

但是这段代码不起作用,当我尝试创建JVM时失败了。我想这是因为它不支持我尝试的选项。

1 个答案:

答案 0 :(得分:1)

所以正确的选项如下

JavaVMOption* options = new JavaVMOption[2];   // JVM invocation options
options[0].optionString = (char *)"--module-path=.;./lib2";   // where to find java .class
options[1].optionString = (char *)"--add-modules=RemkaAgentService,spring.context";

vm_args.version = JNI_VERSION_9;             // minimum Java version
vm_args.nOptions = 2;                          // number of options
vm_args.options = options;
vm_args.ignoreUnrecognized = false;     // invalid options make the JVM init fail