简体   繁体   English

JNI FindClass返回NULL-非常奇怪

[英]JNI FindClass returns NULL - very strange

I know, that there are a lot of the similar questions on https://stackoverflow.com/ , but nothing of them solved my issue. 我知道, https://stackoverflow.com/上有很多类似的问题,但是没有一个解决了我的问题。

I have a simple Java Class without package: 我有一个不带包装的简单Java类:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestConsoleHandler {

    private static Thread hook;

    static File f = new File("c:\\Users\\ArtUrlWWW\\Documents\\NetBeansProjects\\mavenproject1\\shutdown.txt");

    public static void main(String[] args) {

        FileWriter fw = null;
        try {
            try {
                f.createNewFile();
            } catch (Exception ex) {
                Logger.getLogger(TestConsoleHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            fw = new FileWriter(f);
            fw.write("Start \r\n");
            fw.close();
            System.out.println();
            hook = new ShutdownHook();
            Runtime.getRuntime().addShutdownHook(hook);
            replaceConsoleHandler(); // actually not "replace" but "add"
            try {
                Thread.sleep(10000); // You have 10 seconds to close console
            } catch (InterruptedException e) {
            }
        } catch (IOException ex) {
            Logger.getLogger(TestConsoleHandler.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fw.close();
            } catch (IOException ex) {
                Logger.getLogger(TestConsoleHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void shutdown() {
        try {
            FileWriter fw = new FileWriter(f);
            fw.write("Running shutdown \r\n");
            fw.close();

            hook.run();
        } catch (IOException ex) {
            Logger.getLogger(TestConsoleHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static native void replaceConsoleHandler();

    static {
        System.loadLibrary("sdh");
    }
}

and I want to call that class from JNI: 我想从JNI调用该类:

// JavaService.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include "jni.h"

typedef struct JavaVMCreationResult {
    JavaVM* jvm;
    JNIEnv* env;
} JVMCreationResult;

JVMCreationResult* CreateJavaVM() {
    JavaVM* jvm;
    JNIEnv* env;

    JavaVMInitArgs vm_args;
    JavaVMOption* options = new JavaVMOption[1];
    options[0].optionString = "-Djava.class.path=c:/Users/ArtUrlWWW/Documents/NetBeansProjects/mavenproject1/target/";
    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 1;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = JNI_TRUE;
    JNI_GetDefaultJavaVMInitArgs(&vm_args);

    JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);

    JVMCreationResult* cres = new JVMCreationResult();
    cres->jvm = jvm;
    cres->env = env;

    env->GetJavaVM(&jvm);

    jint res = jvm->AttachCurrentThread((void **)(&env), &env);

    return cres;
}


int _tmain(int argc, _TCHAR* argv[])
{

    JVMCreationResult* cres = CreateJavaVM();
    if (!cres) return -1;
    JavaVM* jvm = cres->jvm;
    JNIEnv* env = cres->env;

    if (&env == NULL) {
        printf("env IS NULL!!! \n\n");
    }

    jint res = jvm->AttachCurrentThread((void **)(&env), &env);
    printf("STEP 1 \n\n");
    jclass cls = env->FindClass("TestConsoleHandler");
    printf("STEP 2 \n\n");
    if (cls == NULL) {
        printf("Class IS NULL!!! \n\n");
    }
    printf("STEP 3 \n\n");
    /*jmethodID mid = env->GetStaticMethodID(cls, "shutdown", "()V");
    printf("STEP 4 \n\n");
    env->CallStaticVoidMethod(cls, mid);
    jvm->DetachCurrentThread();*/

    return 0;
}

In the c:/Users/ArtUrlWWW/Documents/NetBeansProjects/mavenproject1/target/ I have 1.jar , that contains needed classes: 在c:/ Users / ArtUrlWWW / Documents / NetBeansProjects / mavenproject1 / target /中,我有1.jar,其中包含所需的类: 在此处输入图片说明

But my programm can't find class TestConsoleHandler : 但是我的程序找不到类TestConsoleHandler 在此处输入图片说明

Please, advice, how to solve this issue? 请,请教,如何解决这个问题?

Thank you very much! 非常感谢你!

The problem was in the code System.loadLibrary("sdh"); 问题出在代码System.loadLibrary("sdh"); . After replacing it by 替换为

System.load("c:/Users/ArtUrlWWW/Documents/NetBeansProjects/mavenproject1/sdh.dll");

all works fine. 一切正常。

As you've already pointed out the answer I'll just add that the function 正如您已经指出的答案,我将添加该函数

System.loadLibrary("sdh");

looks for the file "libsdh.so" which is why it did not work. 查找文件“ libsdh.so”,这就是为什么它不起作用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM