2

I compiled openCV and OpenCV_contrib library and had the resulting build/bin/opencv-411.jar and libraries .so in build/lib/. Now I am wondering how can I add these to my Maven project?

I was using that Maven repository but the contrib modules are not available.

I tried including the jar as a dependency like here with:

<dependency>
    <groupId>org.opencv</groupId>
    <artifactId>opencv-411</artifactId>
    <version>4.1.0</version> 
    <scope>system</scope>
    <systemPath>${resourcesfolder}/opencv-411.jar</systemPath>
</dependency>

I get no error when I launch clean javafx:compile but when I launch clean javafx:run a lot of OpenCV errors appear :

[ERROR] COMPILATION ERROR :

[INFO] -------------------------------------------------------------
[ERROR] /home/.../src/main/java/model/VideoModel.java:[3,23] package org.opencv.core does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[4,23] package org.opencv.core does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[5,26] package org.opencv.videoio does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[6,26] package org.opencv.videoio does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[25,17] cannot find symbol
     symbol:   class VideoCapture
     location: class model.VideoModel
[ERROR] /home/.../src/main/java/model/VideoModel.java:[26,17] cannot find symbol
     symbol:   class Mat

I also had a look to that but he deployed his new jar containing the executables and libraries in a remote repository. The interesting part is the jar creation, so I tried to do the same with opencv-411.jar and lib/ folder with:

cp opencv-411.jar opencv-411-new.jar
jar -uvf opencv-411-new.jar lib/

and kept the dependency as above, but raised the same errors...

What should I do?

Rafutk
  • 129
  • 1
  • 9

3 Answers3

2

Thanks to the answers, I came to the solution for including OpenCV jar and libs to Maven:

  • install the jar in the maven local repository with:
mvn install:install-file -Dfile=/home/.../src/main/resources/opencv.jar -DgroupId=org -DartifactId=opencv -Dversion=4.1.1 -Dpackaging=jar
  • create the dependency in pom.xml:
<dependency> 
 <groupId>org</groupId> 
 <artifactId>opencv</artifactId>
 <version>4.1.1</version>  
</dependency> 

Now that the jar is included, we must add the OpenCV libraries somehow.

Just load the library in your app with:

System.load("/path/to/lib/libopencv_java411.so");

For the UI tests, as the maven-surefire-plugin uses a special JVM, you have to specify the lib folder to the java.library.path:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.22.2</version>
 <configuration>
  <argLine>-Djava.library.path=${project.build.outputDirectory}/lib</argLine>
 </configuration>
</plugin>

Please feel free to edit if you have a better solution or corrections to add.

Rafutk
  • 129
  • 1
  • 9
0

Maven expects the artifacts (.jar files, etc.) in $USER_HOME/.m2 since you compiled opencv yourself you also need to install the file (as 3rd party module) in the local maven repository:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

Where the variables need to match those you use in you pom.xml and packaging must be set to jar.

See also: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

stacker
  • 64,199
  • 27
  • 132
  • 206
  • Thanks for the quick answer, I did the command `mvn install:install-file -Dfile=/home/.../src/main/resources/opencv.jar -DgroupId=org -DartifactId=opencv -Dversion=4.1.1 -Dpackaging=jar` and removed scope and systemPath from the dependency. But I get "Error: Could not find or load main class" from javafx-maven-plugin, it was working before.. – Rafutk Jul 23 '19 at 10:35
  • In your pom snippet you're refering to version 4.1.0 (not 4.1.1) you could try mvn -x (for more detailed output) – stacker Jul 23 '19 at 12:20
  • The version was also modified so it is not the problem. I added the OpenCV library path to java.lib.path with `-Djava.library.path=/home/.../target/classes/lib` to maven-surefire-plugin and the `clean package` maven goals worked (launched every test)! But the `clean javafx:run` still throw the "Could not find or load main class ..." – Rafutk Jul 23 '19 at 14:27
0

mvn install:install-file -Dfile=C:\Users\your jar path -DgroupId=org.opencv -DartifactId=opencv-411 -Dversion=4.1.0 -Dpackaging=jar you can use this one may be solve your issue

Kamal Kumar
  • 194
  • 11
  • Thanks for your answer, I did that command, changed the dependency and added the OpenCV libs path to maven-surefire-plugin. The `package` goal works perfectly, but the `javafx:run` does not work because it doesn't find the main class.. – Rafutk Jul 23 '19 at 14:41