线程“主”中的异常java.lang.UnsatisfiedLinkError:com.printer.PrinterWinAPI.GetStatus(Ljava / lang / String;)J

时间:2018-11-13 16:27:46

标签: java eclipse java-native-interface

这是我的问题:我试图在Java项目中使用JNI作为与Win32 API进行通信的一种方式。我在运行时在Eclipse中收到此错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.printer.PrinterWinAPI.GetStatus(Ljava/lang/String;)J
at com.printer.PrinterWinAPI.GetStatus(Native Method)
at com.printer.PrinterWinAPI.<init>(PrinterWinAPI.java:14)
at com.printer.PrinterWinAPI.main(PrinterWinAPI.java:25)

但是我的问题真正奇怪的是,我可以使用cmd成功编译我的项目:

javac PrinterWinAPI.java

然后运行以下程序即可正常运行

java PrinterWinAPI

据我了解,eclipse成功找到了我的.dll文件,但在文件内部找不到GetStatus()函数。我试图在x86和x86_64中使用Visual Studio以及在x86和x86_64中使用mingw gcc进行编译。这是我的文件:

实现JNI接口的java文件: PrinterWinAPI.java

package com.printer;

public class PrinterWinAPI {
    static {
        System.load("C:\\GetPrinterStatus.dll");
    }

    public Long         status;
    public String       name;

    public PrinterWinAPI(String printerName) {
        this.name = printerName;
        this.status = this.GetStatus(this.name);
    }

    private native long GetStatus(String str);

    public static void main(String[] args) {
        PrinterWinAPI printer = new PrinterWinAPI("PRINTER NAME EXAMPLE");
        System.out.println(printer.status);
    }
}

PrinterWinAPI.h,使用javah PrinterWinAPI生成:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h> /* Header for class PrinterWinAPI */

#ifndef _Included_PrinterWinAPI
#define _Included_PrinterWinAPI
#ifdef __cplusplus extern "C" {
#endif /*  * Class:     PrinterWinAPI  * Method:    
GetStatus  * Signature: (Ljava/lang/String;)J  */ 
JNIEXPORT jlong JNICALL Java_PrinterWinAPI_GetStatus   (JNIEnv *, jobject, jstring);

#ifdef __cplusplus }
#endif
#endif

这是PrinterWinAPI.c文件本身:

#include "PrinterWinAPI.h"   // Generated
#include <windows.h>
#include <wincon.h>
#include <winspool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <io.h> 

// Implementation of the native method
JNIEXPORT jlong JNICALL Java_PrinterWinAPI_GetStatus(JNIEnv *env, 
jobject thisObj, jstring str) {
        //some stuff...
    return (int64_t)dwStatus;
 }

再一次,该程序可以在命令提示符下使用javac,javah和java进行编译并正常运行,我99%的确定问题是由于eclipse引起的,但我真的不知道从哪里开始。 我花了几个小时在网上寻找解决方案,但找不到任何东西。为什么Eclipse无法运行我的项目,但是手动运行Java二进制文件却可以?

我的jre / jdk是1.8.0_101-b13 64位版本

Eclipse:Oxygen.3a发行版(4.7.3a)

操作系统:Windows 7 64位

1 个答案:

答案 0 :(得分:0)

@ user2543253谢谢你的工作!之所以正确编译,是因为我的java文件不在软件包中,所以标头是正确的,但是由于java文件嵌套在我项目的软件包中,所以正确的标头是BubbleShowCaseBuilder(this) //Activity instance .title("foo") //Any title for the bubble view .targetView(view) //View to point out .show() //Display the ShowCase 而不是Java_com_printer_PrinterWinAPI_GetStatus。 / p>

相关问题