导出的(Eclipse)可执行jar文件中的UnsatisfiedLinkError [英] UnsatisfiedLinkError in exported (Eclipse) executable jar file
问题描述
从Eclipse执行代码时,代码工作正常。我正在使用OpenCV 2.4.11和JavaFX for UI。当我从Eclipse导出一个Executable Jar并从cmd运行它时,我得到以下异常:
我在SO和OpenCV论坛上关注了很多帖子(
异常发生在System.loadLibrary(Core.Native_Library_Name),我检查了Native_Library_Name,OpenCV版本与我在项目中导入的版本相同。
公共类CustomFrame扩展Application {
@Override
public void start(Stage primaryStage){
Group root = new Group();
Canvas canvas = new Canvas(1440,840);
ImageView imageView = new ImageView();
imageView.setFitHeight(canvas.getHeight());
imageView.setFitWidth(canvas.getWidth());
new FrameController()。startCamera(imageView);
root.getChildren()。addAll(imageView,canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String [] args)
{
//加载本机OpenCV库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME) );
launch(args);
}
}
如果有人认为我错过了什么请放手我知道。
当应用程序尝试尝试时,会抛出 UnsatisfiedLinkError
在Linux中加载本地库,如
-
.so
, -
.dll
在Windows上或 -
.dylib
在Mac
和该库不存在。
具体来说,为了找到所需的本机库,JVM同时查找 PATH环境变量
和 java.library.path
系统属性。
有时如果本机库已经存在由应用程序
加载并且同一个应用程序尝试再次加载它,这也会导致
错误。
< hr>
首先,我们必须验证<$ c中传递的参数$ c> System.loadLibrary 方法是正确的,并且该库实际存在。请注意,不需要扩展库。因此,如果您的库名为 SampleLibrary.dll
,则必须将SampleLibrary值作为参数传递。
此外,如果您的应用程序已经加载了库,并且应用程序尝试再次加载它,则JVM将抛出 UnsatisfiedLinkError
。此外,您必须验证本机库是否存在于 java.library.path
或 PATH环境库中
你的申请。 如果仍然找不到库,请尝试提供System.loadLibrary方法的绝对路径。
为了执行您的应用程序,使用 -Djava.library.path
参数来显式指定本机库。例如,使用终端(Linux或Mac)或命令提示符(Windows),通过发出以下命令来执行您的应用程序:
java -Djava.library.path =< path_of_your_application> -jar< ApplicationJAR.jar>
您错过了实际命令。使用以下
java -Djava.library.path =C:\ Opencv2.1.11 \ openncv \ buildd \\ _java \ x64-jar BlurDetector.jar
或
java -Djava.library.path =C:\Opencv2.1.11\opencv\build\java-jar BlurDetector.jar
而不是你的命令
java -Djava.library.path =C:\ Users \ vivek_elango\Desktop-jar BlurDetector.jar //你给出了错误的应用程序路径
The code works fine when executing from Eclipse. I'm using OpenCV 2.4.11 and JavaFX for UI. When I export an Executable Jar from Eclipse and run it from cmd I get the following exception:
I followed many post here on SO and OpenCV forum(1, 2, 3, 4) but, none of the answers seems to help me.
I have added the OpenCV jar as library and Native Library is linked to /build/java/x64 as suggested in SO answers.
The exception occurs at the System.loadLibrary(Core.Native_Library_Name), I checked the Native_Library_Name and the OpenCV version is same as the one I imported in my project.
public class CustomFrame extends Application{
@Override
public void start(Stage primaryStage){
Group root = new Group();
Canvas canvas = new Canvas(1440, 840);
ImageView imageView = new ImageView();
imageView.setFitHeight(canvas.getHeight());
imageView.setFitWidth(canvas.getWidth());
new FrameController().startCamera(imageView);
root.getChildren().addAll(imageView, canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args)
{
// load the native OpenCV library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
launch(args);
}
}
If anybody thinks that I have missed something please do let me know.
The UnsatisfiedLinkError
is thrown when an application attempts to load a native library like
.so
in Linux,.dll
on Windows or.dylib
in Mac
and that library does not exist.
Specifically, in order to find the required native library, the JVM looks in both the PATH environment variable
and the java.library.path
system property.
Sometimes if the native library was already loaded by an application and the same application tries to load it again, this can cause this error also.
How to deal with the UnsatisfiedLinkError?
First of all we must verify that the parameter passed in the System.loadLibrary
method is correct and that the library actually exists. Notice that the extension of the library is not required. Thus, if your library is named SampleLibrary.dll
, you must pass the SampleLibrary value as a parameter.
Moreover, in case the library is already loaded by your application and the application tries to load it again, the UnsatisfiedLinkError
will be thrown by the JVM. Also, you must verify that the native library is present either in the java.library.path
or in the PATH environment library
of your application. If the library still cannot be found, try to provide an absolute path to the System.loadLibrary method.
In order to execute your application, use the -Djava.library.path
argument, to explicitly specify the native library. For example, using the terminal (Linux or Mac) or the command prompt (Windows), execute your application by issuing the following command:
java -Djava.library.path= "<path_of_your_application>" –jar <ApplicationJAR.jar>
You have missed the actual command. Use the following
java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java\x64" -jar BlurDetector.jar
or
java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java" -jar BlurDetector.jar
instead of your command
java -Djava.library.path="C:\Users\vivek_elango\Desktop" -jar BlurDetector.jar // you have given wrong path of your application
这篇关于导出的(Eclipse)可执行jar文件中的UnsatisfiedLinkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!