简体   繁体   English

JNI不满意的链接错误

[英]JNI UnsatisfiedLinkError

I want to create a simple JNI layer. 我想创建一个简单的JNI层。 I used Visual studio 2008 to create a dll (Win 32 Console Application project type with DLL as the option). 我使用Visual Studio 2008创建了一个dll(带有DLL作为选项的Win 32 Console Application项目类型)。 Im getting this exception when I invoke the native method: 我在调用本地方法时收到此异常:

Exception occurred during event dispatching:
java.lang.UnsatisfiedLinkError: com.tpd.vcdba.console.TaskScheduler.vcdbaTaskSch
edulerNative.Hello()V
        at com.tpd.vcdba.console.TaskScheduler.vcdbaTaskSchedulerNative.Hello(Na
tive Method)
        at com.tpd.vcdba.console.TaskScheduler.vcdbaTaskSchedulerUtil.isTaskExis
ts(vcdbaTaskSchedulerUtil.java:118)
        at com.tpd.vcdba.console.Dialogs.schedulerWizardPage.scheduleTaskPage.wz
Finish(scheduleTaskPage.java:969)
        at com.tpd.vcdba.console.wizard.vcdbaWizard.gotoFinish(vcdbaWizard.java:
434)
        at com.tpd.vcdba.console.wizard.wzActionPanel.actionPerformed(wzActionPa
nel.java:163)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

The header file generated is : 生成的头文件是:

/* DO NOT EDIT THIS FILE - it is machine generated */

    #include <jni.h>
    /* Header for class com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative */

    #ifndef _Included_com_tpd_vcdba_console_TaskScheduler_
    vcdbaTaskSchedulerNative
    #define _Included_com_tpd_vcdba_console_TaskScheduler_
    vcdbaTaskSchedulerNative
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative
     * Method:    Hello
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative_Hello
      (JNIEnv *, jobject);

    #ifdef __cplusplus
    }
    #endif
    #endif

The implementation file is: 实现文件是:

#pragma once
#include "com_tpd_vcdba_console_TaskScheduler_
vcdbaTaskSchedulerNative.h"
#include "stdafx.h"
#include "jni.h"

/*
 * Class:     com_tpd_vcdba_console_TaskScheduler_vcdbaTaskScheduler_native
 * Method:    Hello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative_Hello
  (JNIEnv *envs, jobject obj){
    printf("hello world");
}

The java file is: Java文件是:

package com.tpd.vcdba.console.TaskScheduler;


import com.tpd.vcdba.console.TaskScheduler.ScheduleTask;

public class vcdbaTaskSchedulerNative {


    public native void Hello();
    private static vcdbaTaskSchedulerNative instance = null;

    static{
        try{
            System.loadLibrary("JNITrial");

        }
        catch(Exception ex){

        }

    }

    public vcdbaTaskSchedulerNative(){

    }

    public static vcdbaTaskSchedulerNative getInstance(){
        if(instance == null){
            instance = new vcdbaTaskSchedulerNative();
        }
        return instance;
    }

}

When I invoke the native method "Hello" i get the execption. 当我调用本地方法“ Hello”时,我得到执行。

Another thing I observed is that when I compile in command line using: “cl -I"C:\\Program Files (x86)\\Java\\jdk1.7.0\\include" -I"C:\\Program Files (x86)\\Java\\jdk1.7.0\\include\\win32" -LD "C:\\Users\\administrator.RMDOM\\Documents\\Visual Studio 2008\\Projects\\JNITrial\\JNITrial\\JNIInt.cpp" -FeJNITrial.dll” , everything works fine. 我观察到的另一件事是,当我使用以下命令在命令行中进行编译时:“ cl -I” C:\\ Program Files(x86)\\ Java \\ jdk1.7.0 \\ include“ -I” C:\\ Program Files(x86)\\ Java \\ jdk1.7.0 \\ include \\ win32“ -LD” C:\\ Users \\ administrator.RMDOM \\ Documents \\ Visual Studio 2008 \\ Projects \\ JNITrial \\ JNITrial \\ JNIInt.cpp“ -FeJNITrial.dll”,一切正常。

Am I missing out something in Visual Studio Settings? 我是否在Visual Studio设置中缺少某些内容? I have option Use of MFC as "Use MFC in a Shared DLL", Code generation option as "Multi-threaded DLL (/MD)". 我有使用MFC作为“在共享DLL中使用MFC”的选项,有代码生成选项作为“多线程DLL(/ MD)”。 Its a 64 bit dll. 它是一个64位dll。 Is there something else that I need to add? 还有什么我需要补充的吗?

Any help is welcome. 欢迎任何帮助。 Thanks in advance. 提前致谢。

Could you tell me what kind of JVM are you using, 32 or 64-bit? 您能告诉我您使用的是哪种32位或64位JVM吗? Your library is 640bit dll, but in your path I can see C:\\Program Files (x86)... so maybe this is the problem. 您的库是640位dll,但是在您的路径中我可以看到C:\\ Program Files(x86)...所以也许是问题所在。

I figured out the solution. 我想出了解决方案。

My project had use precompiled headers option set, so the compiler was skipping the statement: 我的项目使用了预编译头选项集,因此编译器正在跳过以下语句:

#include "com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative.h"

Once I removed that option it worked like magic. 一旦我删除了该选项,它就像魔术一样工作。

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

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