JNI加载具有依赖项的jar包

4

我正在尝试使用JNI在我的C++程序中加载以下Java类:

package helloWorld;

import org.apache.log4j.Logger;

public class HelloWorld{

    private static final Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String[] args){
        System.out.println("Hello, World");
    }
    public static int square(int input){
        int output = input * input;
        return output;
    }
    public static int power(int input, int exponent){
        int output,i;
        output=1;
        for(i=0;i<exponent;i++){
            output *= input;
        }
        return output;
    }
}

它依赖于log4j-1.2.16.jar。

这是我的C++代码:

#include <stdio.h>
#include <cstdlib>
#include "../Header/jni.h"

JNIEnv* create_vm(JavaVM **jvm)
{
    char * classPath = (char *) "-Djava.class.path=HelloWorld-0.0.1-SNAPSHOT.jar";
    JNIEnv* env;
    JavaVMInitArgs args;
    JavaVMOption options[2];
    args.version = JNI_VERSION_1_6;
    args.nOptions = 1;
    options[0].optionString = classPath;
    options[1].optionString = "-verbose";
    args.options = options;
    args.ignoreUnrecognized = 0;
    int rv;
    rv = JNI_CreateJavaVM(jvm, (void**)&env, &args);
    if (rv < 0 || !env)
        printf("Unable to Launch JVM %d\n",rv);
    else
        printf("Launched JVM! :)\n");
    return env;
}

void invoke_class(JNIEnv* env)
{
    jclass hello_world_class;
    jmethodID main_method;
    jmethodID square_method;
    jmethodID power_method;
    jint number=20;
    jint exponent=3;

    hello_world_class = env->FindClass("helloWorld/HelloWorld");

    if(hello_world_class == NULL){
        if(env->ExceptionOccurred()){
            env->ExceptionDescribe();
        }
        printf("Class not found.");
    }
    else{
        main_method = env->GetStaticMethodID(hello_world_class, "main", "([Ljava/lang/String;)V");
        square_method = env->GetStaticMethodID(hello_world_class, "square", "(I)I");
        power_method = env->GetStaticMethodID(hello_world_class, "power", "(II)I");
        env->CallStaticVoidMethod(hello_world_class, main_method, NULL);
        printf("%d squared is %d\n", number,
                env->CallStaticIntMethod(hello_world_class, square_method, number));
        printf("%d raised to the %d power is %d\n", number, exponent,
                env->CallStaticIntMethod(hello_world_class, power_method, number, exponent));
    }
}

int main(int argc, char **argv)
{
    JavaVM *jvm;
    JNIEnv *env;
    env = create_vm(&jvm);
    if(env == NULL)
        return 1;
    invoke_class(env);
    system("PAUSE");
    return 0;
}

我已经将HelloWorld.jar放置在我的C++应用程序的根目录中。当它尝试加载hello_world_class时,会抛出以下异常:

java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at helloWorld.HelloWorld.<clinit>(HelloWorld.java:7)

JNI无法找到log4j依赖项,因为它不在HelloWorld.jar内。我尝试将它放在lib文件夹和与HelloWorld.jar相同的文件夹中,但没有成功。我需要把log4j.jar放在哪里,这样JNI才能识别并加载它呢?
非常感谢,我是JNI的新手,请您的回答要清晰易懂。我已经困扰了一整天T.T

如果这是C++代码,为什么你打了C的标签? - PC Luddite
抱歉,已删除标签。 - Victor Silva Do Nascimento
你已经明确指定了类部分,为什么不列出两个“jar”呢? - Alan Stokes
@AlanStokes,您是指声明另一个包含log4j jar路径的classPath吗?但是jni会理解它是HelloWorld.jar的依赖项吗?我该如何将其链接为HelloWorld的依赖项? - Victor Silva Do Nascimento
4
(或在Linux上使用冒号而非分号)"-Djava.class.path=foo.jar;baz.jar"。它不关心依赖项,只需知道在需要时在哪里查找每个类。 - Alan Stokes
1个回答

1

您只需要在选项中传递JAR文件的位置即可。您可以像这样做:

options[0].optionString = "-Djava.class.path=./target:../target:./jar:./jar/log4j-api-2.12.1.jar:./jar/log4j-core-2.12.1.jar";

请记住,您可能还需要log4j2.xml(在log4j-2.12.1的情况下)。为确保可以发现配置文件,请将包含该文件的目录添加到类路径中。
您可以在此处找到完整的示例代码:https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo060

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接