在Windows上加载多个Jar-JNI JNI_CreateJavaVM [英] Loading Multiple Jars on Windows - JNI JNI_CreateJavaVM

查看:149
本文介绍了在Windows上加载多个Jar-JNI JNI_CreateJavaVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows的C ++应用程序中创建一个JVM,但无法说服它加载多个jar。

I'm creating a JVM within my C++ application for windows, and I'm unable to convince it to load multiple jars.

我的C ++代码:

MyClass::MyClass(std::string & classPath) {
    classPath = "-cp "+classPath;   // <--  Won't work with any path or single jar
    //classPath = "-Djava.class.path="+classPath; <-- Only works with single jar
    jvmOptions[0].optionString = (char *)classPath.c_str();
    jvmOptions[1].optionString = "-Xms8m";
    jvmOptions[2].optionString = "-Xmx24m";
    jvmArgs.version = JNI_VERSION_1_6;
    jvmArgs.options = jvmOptions;
    jvmArgs.nOptions = 3;
    jvmArgs.ignoreUnrecognized = JNI_TRUE;
    int jvmInitResult = CreateJavaVM( &jvm, (void**)&environment, &jvmArgs);

    if( jvmInitResult >= 0 ) {
        jclass loadedClass = environment->FindClass( MyClassName.c_str() );
          .....

如果我通过 classPath 变量添加到单个JAR,例如 C:\path\myjar.jar jclass 变量位置很好。但是,我的Java类需要其他JAR才能起作用,因此我需要将多个JAR传递给 jvmOptions 。当我尝试通过以下任何一种方式传递第二个或第三个JAR时, FindClass 调用现在会失败。

If I pass a path via my classPath variable to a single JAR, such as "C:\path\myjar.jar", the jclass variable is located fine. However, my Java class requires additional JARs to function, so I need to pass more than one JAR to the jvmOptions. When I try to pass the second, or third JAR, in any of the following ways, the FindClass call now fails.

C:\path\myjar.jar    <--------- FindClass SUCCESS; can't use due to missing jars
C:\path\myjar.jar;C:\path\secondjar.jar  <-----FindClass FAIL
C:\path\myjar.jar:C:\path\secondjar.jar  <-----FindClass FAIL
C:\path\*  <-----FindClass FAIL
C:\path\*.jar  <-----FindClass FAIL
"C:\path\myjar.jar;C:\path\secondjar.jar"  <-----FindClass FAIL
"C:\path\myjar.jar:C:\path\secondjar.jar"  <-----FindClass FAIL

我认为我没有想到其他选择,但这使我发疯。

I assume there is another option I'm not thinking of, but this is driving me nuts.

推荐答案

解决方案是在将参数传递给程序时不使用Windows文件分隔符。 \最终得到一个或多个escape的转义。将参数更改为UNIX样式文件分隔符可以正确加载目录中的所有jar。

The solution is to not use windows file separators when passing the argument to the program. The \ ends up getting escape sequenced with one or more \ . Changing the argument to unix style file separators correctly loads all of the jars within a directory.

例如:

MyApp "classpath"
MyApp C:\pathtojars\  <-- fails
MyApp C:/pathtojars/  <-- works

固定代码:

MyClass::MyClass(std::string & classPath )
{
   classPath = "-Djava.class.path="+classPath;
   jvmOptions[0].optionString = (char *)classPath.c_str();
   jvmOptions[1].optionString = "-Xms8m";
   jvmOptions[2].optionString = "-Xmx24m";
   jvmArgs.version = JNI_VERSION_1_6;
   jvmArgs.options = jvmOptions;
   jvmArgs.nOptions = 3;
   jvmArgs.ignoreUnrecognized = JNI_TRUE;
   int jvmInitResult = CreateJavaVM( &jvm, (void**)&environment, &jvmArgs);

   if( jvmInitResult >= 0 )
   {
       jclass loadedClass = environment->FindClass( MyClassName.c_str() );
        .....

这篇关于在Windows上加载多个Jar-JNI JNI_CreateJavaVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆