简体   繁体   English

如何从命令行(Unix)将外部库(OpenCV .jar文件)添加到Java构建路径

[英]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.

相关问题 如何通过添加外部Jar并使用文件的完整路径从命令行编译和执行Java? - How can I compile and execute Java from command line adding external Jar and using file's full path? 如何从命令行将jar文件添加到Eclipse Java项目中? - How to add a jar file to an eclipse java project from command line? 从 unix 命令行运行 jar 文件 - Run a jar file from unix command line 如何从Eclipse编译的命令行运行Java类文件?外部Jar问题 - How to Run a Java Class File from the Command Line that was Compiled by Eclipse? External Jar problems 如何从我的java类构建库文件(jar文件)? - how to build a library file (jar file) from my java classes? 如何使用以下命令行在类 UNIX 中执行 jar 文件:java -jar mysolution.jar &lt; inputfile.txt - How can I execute jar file in UNIX-like using this command line: java -jar mysolution.jar < inputfile.txt 带有外部 .jar 的 Java 命令行 - Java command line with external .jar Netbeans:如何构建与外部.jar库一起附加的.jar文件? - Netbeans: How to build .jar file attached with external .jar library? java.util.MissingResourceException-新File()不能进入jar-如何为外部库提供此File的路径? - java.util.MissingResourceException - new File() does not reach into jar - how to provide external library with a path for this File? Java:如何从命令行导入jar文件 - Java: how to import a jar file from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM