[2.0.4] SikuliX API: Error with runnable fat jar --- opencv dll not available at runtime

Asked by Ralph

Hi all,

When I export as a fat jar and run on another Win10 PC with Java installed I get the following error when I use screen.exist, hover, click etc

java.lang.UnsatisfiedLinkError: 'long org.opencv.core.Mat.n_Mat()'

Pom is below. Just wondering if you have seen this before or have any suggestions? Note I'm running 2.0.4 because 2.0.5 has an issue locating images embedded in the jar

Thanks in advance

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.rnimbin.autotest</groupId>
  <artifactId>DailyChecks</artifactId>
  <version>0.0.1-SNAPSHOT</version>

<properties>
  <selenium.version>3.13.0</selenium.version>
  <testng.version>7.4.0</testng.version>
  <maven.compiler.version>3.7.0</maven.compiler.version>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>${selenium.version}</version>
  </dependency>
  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>${testng.version}</version>
  </dependency>
  <dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>4.0.6</version>
  </dependency>
  <dependency>
    <groupId>com.sikulix</groupId>
    <artifactId>sikulixapi</artifactId>
    <version>2.0.4</version>
  </dependency>
  <dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.17.2</version>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>utils.MainClass</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

</project>

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

ok, I will check/test and make you a suggestion for a workaround asap.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

put the following at the top of your main class:

static { new Finder(new Screen().capture(0, 0, 1, 1)); }

Looks a bit odd, but it takes care, that the <appdata>\Sikulix folder is created and the OpenCV DLL is exported and loaded, before any SikuliX features are touched.

Revision history for this message
Ralph (rnimbin) said :
#3

Hi, thanks heaps but still get a similar error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: 'long org.opencv.core.Mat.n_Mat()'
        at org.opencv.core.Mat.n_Mat(Native Method)
        at org.opencv.core.Mat.<init>(Mat.java:30)
        at org.sikuli.script.Finder$FindInput2.<init>(Finder.java:1167)
        at org.sikuli.script.Finder$FindInput2.<init>(Finder.java:1161)
        at org.sikuli.script.Finder.<init>(Finder.java:34)
        at utils.MainClass.<clinit>(MainClass.java:25)

The PC I am running on is Win10 and has Java DevKit 17.0.2 installed. The Java environment variables are set and I have copied opencv_java342.dll to various system folders in the path to see if that helps.

The jar runs standalone without error on the IDE PC which has the same OS and same version/configuration of Java. The only solutions installed from the eclipse marketplace are Maven (Eclipse m2e) and TestNG.

I have tried creating the fat jar with maven-assembly-plugin and maven-shade-plugin and get the same error

The following folders exist under <appdata>\roaming\sikulix
Extensions
SikulixDownloads
SikulixLibs (contains opencv_java342.dll)
SikulixStore
SikulixTesseract

Revision history for this message
Ralph (rnimbin) said (last edit ):
#4

[update] I replaced "static { new Finder(new Screen().capture(0, 0, 1, 1)); }" with System.loadLibrary(Core.NATIVE_LIBRARY_NAME) and copied opencv_java342.dll to <java>\bin and the error is gone now. Application now works on the non-IDE PC :)

Revision history for this message
RaiMan (raimund-hocke) said :
#5

That is really odd.

... and I do not understand it :-(

I cannot reproduce it here, because I only have my one dev-Win11 available, where everything works fine, including the case with the fat jar from command line.

I do not have any Java specific environment variables set - running java is done with a command-file, that executes the exe from Java's .../bin folder.

This is the equivalent in SikuliX to your solution (copy to Java/bin and System.loadLibrary()):
RunTime.loadLibrary(RunTime.libOpenCV)

the add-on is the export of the libs folder to <appdata>.
And the difference is that I use System.load(<absolute-path-to-opencv-dll-in-appdata>).
For some reason this silently does not make the dll available.

For now, I cannot do anything else, but will add an early test in 2.0.6, wether the System.load() was successful and the dll is available for opencv.java.

One more thing:
It would be helpful for me to get the source of your failing main class.
If that is ok for you, then send it per mail: sikulix---at---outlook---dot---com.
If not, no problem.

Thanks for your time anyways.

Can you help with this problem?

Provide an answer of your own, or ask Ralph for more information if necessary.

To post a message you must log in.