[英]How to add an external library (OpenCV .jar file) to the Java build path from the command line (Unix)
I am trying to add the OpenCV library to the build path for my Java program from the terminal so that I can use a shell script to run the program on a bunch of image files sequentially. 我试图从终端将OpenCV库添加到Java程序的构建路径中,以便可以使用Shell脚本按顺序在一堆图像文件上运行该程序。 The .class files for my project are located in Documents/Programming/Ko/bin and the .jar file for the OpenCV library is located in Documents/Programming/opencv-2.4.10/build/bin .
我项目的.class文件位于Documents / Programming / Ko / bin中 ,而OpenCV库的.jar文件位于Documents / Programming / opencv-2.4.10 / build / bin中 。 As suggested from a number of different questions on here, I have tried (while in the Ko/bin directory):
正如在这里的许多其他问题所建议的那样,我已经尝试过(在Ko / bin目录中):
java -cp "/Users/jordan/Documents/Programming/opencv-2.4.10/build/bin/*:." Ko <image_name>
Which produces the following error: 产生以下错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java2410 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at Ko.main(Ko.java:37)
So the program runs into an issue at line 37. This line is: 因此该程序在第37行遇到问题。该行是:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
I added this line after running into issues loading the OpenCV library in Eclipse, from the advice of another question on here, although I'm not exactly sure what it does or why it is necessary. 我在这里遇到另一个问题的建议后遇到了在Eclipse中加载OpenCV库的问题后添加了这一行,尽管我不确定它是做什么的,还是不确定为什么这样做。 When I try commenting out this line I get a similar error:
当我尝试注释掉这一行时,出现类似的错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0(Ljava/lang/String;I)J
at org.opencv.highgui.Highgui.imread_0(Native Method)
at org.opencv.highgui.Highgui.imread(Highgui.java:309)
at Ko.main(Ko.java:39)
This time at line 39, which logically is the first time I call a method from the OpenCV library. 这次是在第39行,这在逻辑上是我第一次从OpenCV库中调用方法。
As far as I can tell, the way I'm writing the java -cp ...
command is identical to what has worked for others from the questions I've read on here. 据我所知,我编写
java -cp ...
命令的方式与我在这里阅读的问题对其他人的工作方式相同。 If anyone could tell me where I'm going wrong and could explain why the call to System.loadLibrary() is necessary in this particular case when in other cases of importing libraries it is not, it would be greatly appreciated. 如果有人能告诉我我哪里出问题了,并且可以解释为什么在这种特殊情况下需要调用System.loadLibrary()而在其他导入库的情况下则没有必要,那么将不胜感激。
The problem was that the command line had no knowledge of where the native libraries were on disk (ie the opencv2.so file). 问题是命令行不知道本地库在磁盘上的位置(即opencv2.so文件)。 After adding an extra flag to the command and specifying the path to the native libraries, the program executed correctly:
在命令中添加了额外的标志并指定了本机库的路径后,程序正确执行:
java -D'java.library.path=<path to native libraries>' -cp '<path to Ko.class>:<path to opencv-2.4.x jar file>' Ko <path to input image file>
The first flag (-D) sets the java.library.path variable to the native libraries specific to this application so that the JRE knows where the jar file is linking to. 第一个标志(-D)将java.library.path变量设置为特定于此应用程序的本机库,以便JRE知道jar文件链接到的位置。 The second flag (-cp) sets the class path (which directories the JRE looks in for files to run).
第二个标志(-cp)设置类路径(JRE在其中查找要运行的文件的目录)。 These directories are separated by colons (:) on OSX/Linux/Unix and separated by semicolons (;) on Windows.
这些目录在OSX / Linux / Unix上用冒号(:)分隔,在Windows上用分号(;)分隔。
Hope this helps anyone who runs across this problem in the future. 希望这对将来遇到此问题的任何人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.