退出Java JNI,java.lang.UnsatisfiedLinkError加载dll [英] Ecplise Java JNI, java.lang.UnsatisfiedLinkError loading dll

查看:121
本文介绍了退出Java JNI,java.lang.UnsatisfiedLinkError加载dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加载打印机dll时遇到问题.我有打印机制造商(JniPrinterStatusLib.dll)的dll文件.我写了像打印机制造商建议的代码.代码是:

I'm having a problem with loading printer dll. I have a dll file from the printer manufacturer (JniPrinterStatusLib.dll). I wrote code like printer manufacturer suggested. The code is:

package com.printer.test

public class JniPrinterStatus {
    static{
        System.loadLibrary("JniPrinterStatusLib");
    }

    public native int GetStatus(String printer);
}

package com.printer.test

public class TestSample {
    public static void main(String[] args) {
        int status;
        String printer = "MY PRINTER";
        JniPrinterStatus jps = new JniPrinterStatus();

        System.out.println("PRINTER NAME = " + printer);

        status = jps.GetStatus(printer);
        if (status == -1) {
            System.out.println("status = -1");
        }
        else if (status == 0) {
            System.out.println("status = NORMAL");
        }
        else if ((status & 0x00000080) != 0) {
            System.out.println("status = PRINTER_STATUS_OFFLINE");
        }
        else if ((status & 0x00400000) != 0) {
            System.out.println("status = PRINTER_STATUS_DOOR_OPEN");
        }
        else if ((status & 0x00000010) != 0) {
            System.out.println("status = PRINTER_STATUS_PAPER_OUT");
        }
        else if ((status & 0x00000800) != 0) {
            System.out.println("status = PRINTER_STATUS_OUTPUT_BIN_FULL");
        }
        else if ((status & 0x00000040) != 0) {
            System.out.println("status = PRINTER_STATUS_PAPER_PROBLEM");
        }
   }
}

我使用Eclipse运行代码,将dll库放入文件夹项目中,错误是

I used Eclipse to run the code, i put the dll library in the folder project and the error is

PRINTER NAME = MY PRINTER
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.printer.test.JniPrinterStatus.GetStatus(Ljava/lang/String;)I
    at com.printer.test.JniPrinterStatus.GetStatus(Native Method)
    at com.printer.test.TestSample.main(TestSample.java:10)

如果我将源从"com.printer.test"包移动到默认包,则代码会工作并显示:

If i move the source from the package "com.printer.test" to default package the code works and show:

PRINTER NAME = MY PRINTER
status = -1

我不知道这怎么可能.如果我在没有包的情况下从命令提示符下编译并运行代码,那么它将起作用.

I don't know how it's possible. If i compile and run the code from command prompt without package it works.

问题出在哪里?

谢谢

推荐答案

javadoc 中获取类UnsatisfiedLinkError ...

如果Java虚拟机找不到合适的,则抛出该异常 声明为native的方法的native语言定义.

Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.

这意味着找不到功能Java_com_printer_test_JniPrinterStatus_GetStatus.

方法loadLibrary通常搜索[System]属性"java.library.path"中列出的目录.对于Windows计算机,此属性的值通常是PATH环境变量的值.

Method loadLibrary in class java.lang.System usually searches the directories listed in the [System] property "java.library.path". For Windows machines, the value of this property is generally the value of the PATH environment variable.

因此,我建议在代码中打印出该属性的值,以查看其是否包含包含DLL的目录.如果不是,那么您需要通过重新定位DLL或更改PATH环境变量或使用-Djava.library.path=...选项启动Java程序来解决此问题.之后,您需要检查本机方法的签名. Dependency Walker 是我在工作中用来完成此任务的工具.

So I suggest printing out the value of that property in your code to see whether it includes the directory containing your DLL. If it doesn't then you need to fix that, either by relocating the DLL or changing the PATH environment variable or launching your java program with the -Djava.library.path=... option. After that you need to check the signature of the native method. Dependency Walker is a tool I use at my work to accomplish this.

编辑 重新阅读了您的问题后,我觉得我没有准确地回答您的问题,所以让我补充一下...

EDIT Having re-read your question, I feel I did not accurately address your question, so let me add...

Eclipse 的默认行为是将资源文件(如DLL)复制到输出文件夹.因此,如果将DLL放在文件夹src\com\printer\test中,它将被复制到文件夹bin\com\printer\test.我的猜测是,当前的工作目录(即.)位于您的"java.library.path"中,这就是为什么当您的Java代码位于默认包中时它可以工作的原因.

The default behaviour of Eclipse is to copy resource files, like DLLs, to the output folder. So if you put your DLL in folder src\com\printer\test, it will get copie to folder bin\com\printer\test. My guess is that the current, working directory, i.e. . is in your "java.library.path" which is why it works when your java code is in the default package.

这篇关于退出Java JNI,java.lang.UnsatisfiedLinkError加载dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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