Search code examples
javaopencvclasspathsikuliunsatisfiedlinkerror

sikuli classpath UnsatisfiedLinkError no opencv_core with macosx intellij Junit


I have done previous searches trying to find an answer to this however my attempts failed so far. I think the error is quite simple its just not loading the classes.

I am running MacOSX 10 with intellij. I am using it with Junit Spring and Maven & Junit.

I followed the maven dependencies found mvnrepository.com - sikuli-api 1.2.0 so I was thinking that if the dependencies are added to the pom then all files should be in my class path? So I don't understand why its not working?

This previous answer looks close to mine - but its for windows im on a mac. However by using maven I should not need to add it to the class path?? or am I missing something. This similar unanswered question also looks similar uses mac like mine

POM Dependencies added

    <dependency>
        <groupId>org.sikuli</groupId>
        <artifactId>sikuli-api</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
        <groupId>org.sikuli</groupId>
        <artifactId>sikuli-core</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
        <version>0.9</version>
    </dependency>
    <dependency>
        <groupId>org.bytedeco.javacpp-presets</groupId>
        <artifactId>opencv</artifactId>
        <version>2.4.9-0.9</version>
        <classifier>macosx-x86_64</classifier>
    </dependency>
    <dependency>
        <groupId>org.piccolo2d</groupId>
        <artifactId>piccolo2d-core</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.piccolo2d</groupId>
        <artifactId>piccolo2d-extras</artifactId>
        <version>1.3.1</version>
    </dependency>

My test

static {
    System.setProperty("platform.dependency", "macosx-x86_64");
    //System.setProperty("platform.dependency", "1");  // tried this also
}

@Test
public void testOne() throws Exception {

    File file = new File(getClass().getClassLoader().getResource("camera_icon.png").getFile());

    browse(new URL("http://code.google.com"));

    ScreenRegion s = new DesktopScreenRegion();
    Target target = new ColorImageTarget(file);

    // ** Fails here  **
    ScreenRegion r = s.find(target); 
    ....

The Error - ClassLoader

I followed the debugger and it fails on the class loader for open_core -- see screenshot

enter image description here

Update

I added the POM classifier per Samuel answer below. I also tried setting the system property. still getting the same error.

Also noticed the following error - I have tried to cut it down as much as possible.

Caused by: java.lang.UnsatisfiedLinkError: /private/var/folders/qp/.../libjniopencv_core.dylib: dlopen(/private/var/....../libjniopencv_core.dylib, 1): Library not loaded: @rpath/libopencv_core.2.4.dylib
  Referenced from: /private/var/.......libjniopencv_core.dylib
  Reason: no suitable image found.  Did find:
    /private/va.....77/./libopencv_core.2.4.dylib: malformed mach-o image: load command #12 length (0) too small in /private/var/fo......./libopencv_core.2.4.dylib  t java.lang.ClassLoader$NativeLibrary.load(Native Method)

Solution

  • For my work around I install opencv via homebrew. Open terminal and type the following.

    brew tap homebrew/science

    brew info opencv

    brew install opencv

    This allowed my POM to be much smaller

    <?xml version="1.0" encoding="UTF-8"?>
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>sikuliTest</groupId>
        <artifactId>sikuliTest</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    
    
        <dependency>
            <groupId>org.sikuli</groupId>
            <artifactId>sikuli-api</artifactId>
            <version>1.2.0</version>
        </dependency>
    
    </dependencies>
    
    </project>
    

    The test

    @Test
    public void testOne() throws IOException {
    
        File file = new File(getClass().getClassLoader().getResource("image_to_click.jpeg").getFile());
        browse(new URL("http://code.google.com"));
    
        // click image that looks like image_to_click.jpeg
        ScreenRegion s = new DesktopScreenRegion(1);
        ScreenRegion s1 = s.find(new ImageTarget(file));
        Mouse mouse = new DesktopMouse();
        mouse.click(s1.getCenter());
    
        // take a screenshot and save it
        BufferedImage img = s.capture();
        File outputfile = new File("screenshot_image.jpg");
        ImageIO.write(img, "jpg", outputfile);
    }