简体   繁体   中英

OpenCV with Java Gradle Project as fat jar

I am actually Trying to use OpenCV in Java based Gradle Project. Since, OpenCV needs native library and Jar File for execution. I am trying to wrap native library and Jar together using gradle, but I am facing errors in doing so.

When I try to run project, project is not able to find native library for opencv jar and giving me below error

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java340 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at Library.(Library.java:9)

Although, I know how to set native library manually in Gradle project but I am not sure how to do it via Gradle and wrap native library in fat jar. Here is my build.gradle

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

configurations {
    // configuration that holds jars to include in the jar
    openCVLibs
}

dependencies {
    openCVLibs fileTree(dir: 'libs', include: '*.jar')
    openCVLibs fileTree(dir: 'libs', include: '*.so')
    configurations.compile.extendsFrom(configurations.openCVLibs)
}

jar {
    from {
            configurations.openCVLibs.collect { it.isDirectory() ? it : zipTree(it) }
    }
    manifest {
        attributes('Implementation-Title': project.name,
                   'Implementation-Version': project.version)
    }
}

have also included link of sample eclipse project

So Here is edit Based on @kerry's comment I tried to crate mvn artifact following openCV Maven , but now I am facing following error while creating mvn build

[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties (set-arch-properties) on project opencv: Properties could not be loaded from File: /media/nitish/8EE47633E4761E21/opencv-3.4.0/build/build.properties -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties (set-arch-properties) on project opencv: Properties could not be loaded from File: /media/nitish/8EE47633E4761E21/opencv-3.4.0/build/build.properties

There is no build.properties file present in build folder. Since build folder is created by maven task only, so build.properties file should be created by maven only.

Following there is a working example of a build.gradle file. Make sure to read the comments and make changes when appropriate. By running gradle fatJar you can create a working Java Jar of your application with OpenCV inside.

However, apart form including your OpenCV library in your Java Jar, you will need to load the OpenCV native file at the beginning of your code. They are two ways to do that:

1) Load the file by providing the full path:

System.load("my/full/path/opencv.dll");

2) If your native file is located inside your Java Library Path:

System.loadLibrary("opencv");

Take notice that in the second case you only need to provide the name of your native file (without its extension).

The default Java Library Path depends on OS:

On Windows, it maps to PATH
On Linux, it maps to LD_LIBRARY_PATH
On OS X, it maps to DYLD_LIBRARY_PATH

If you want to set your own Java Library Path:

    try {
        System.setProperty("java.library.path","YOUR/PATH");
        Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
        fieldSysPath.setAccessible(true);
        fieldSysPath.set(null, null);
    } catch (Exception ex) {
        System.out.println("Failed to set Java Library Path: " + ex.getMessage);
    }

build.gradle

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'

mainClassName = "YourMainClass" // You Main Class name

repositories{
    mavenCentral()
}

task fatJar(type: Jar) {
    manifest.from jar.manifest
    classifier = 'all'
    from {
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
        exclude "build/libs/philipath/**"
    }
    with jar
}

artifacts {
    archives fatJar
}


dependencies {

    // Local libraries
    compile fileTree('lib') // assuming there is a folder named 'lib' in your project root with your OpenCV jar inside

    // other dependencies
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM