简体   繁体   English

Eclipse导出的Runnable Jar无法找到外部库

[英]Eclipse exported Runnable Jar fails to locate external libraries

I have a Java Project with two external libraries. 我有一个带有两个外部库的Java项目。 Everything runs fine in Eclipse, there are no errors when exporting as a runnable JAR. 一切在Eclipse中都运行良好,导出为可运行的JAR时没有错误。 However, when I run my project through the command line using java -jar myapplication.jar, it throws the ever infamous java.lang.UnsatisfiedLinkError: no in java.library.path . 但是,当我使用java -jar myapplication.jar,通过命令行运行项目时java -jar myapplication.jar,它会抛出臭名昭著的java.lang.UnsatisfiedLinkError: no in java.library.path

I have tried using all three export options (extract, package, copy) with no success. 我尝试使用所有三个导出选项(提取,打包,复制)均未成功。

(using eclipse) (使用日食)

In order for System.loadLibrary() to work, the library (on Windows, a DLL) must be in a directory somewhere on your PATH or on a path listed in the java.library.path system property (so you can launch Java like java -Djava.library.path=/path/to/dir). 为了使System.loadLibrary()正常工作,该库(在Windows上为DLL)必须位于PATH上某个目录中或java.library.path系统属性中列出的路径中的某个目录中(因此,您可以像java -Djava.library.path = / path / to / dir)。

Additionally, for loadLibrary(), you specify the base name of the library, without the .dll at the end. 此外,对于loadLibrary(),您可以指定库的基本名称,但结尾不要.dll。 So, for /path/to/something.dll, you would just use System.loadLibrary("something"). 因此,对于/path/to/something.dll,您只需使用System.loadLibrary(“ something”)。

You also need to look at the exact UnsatisfiedLinkError that you are getting. 您还需要查看获得的确切UnsatisfiedLinkError。 If it says something like: 如果显示类似以下内容:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no foo in java.library.path then it can't find the foo library (foo.dll) in your PATH or java.library.path. 线程“主”中的异常java.lang.UnsatisfiedLinkError:java.library.path中没有foo,因此它无法在PATH或java.library.path中找到foo库(foo.dll)。 If it says something like: 如果显示类似以下内容:

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.example.program.ClassName.foo()V then something is wrong with the library itself in the sense that Java is not able to map a native Java function in your application to its actual native counterpart. 线程“主”中的异常java.lang.UnsatisfiedLinkError:com.example.program.ClassName.foo()V,然后库本身出了点问题,因为Java无法将应用程序中的本机Java函数映射到其实际的本地副本。

To start with, I would put some logging around your System.loadLibrary() call to see if that executes properly. 首先,我将在您的System.loadLibrary()调用周围进行一些日志记录,以查看其是否正常执行。 If it throws an exception or is not in a code path that is actually executed, then you will always get the latter type of UnsatisfiedLinkError explained above. 如果它引发异常或不在实际执行的代码路径中,那么您将始终得到上面解释的后一种类型的UnsatisfiedLinkError。

As a sidenote, most people put their loadLibrary() calls into a static initializer block in the class with the native methods, to ensure that it is always executed exactly once: 附带说明一下,大多数人使用本机方法将其loadLibrary()调用放入类中的静态初始化程序块中,以确保始终始终执行一次:

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

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