简体   繁体   中英

How add libraries to my .jar file

I have a project on Netbeans working okay with opencv library.

I have set on project Properties > Run: -Djava.library.path="C:\\opencv\\build\\java\\x64"

If I run the project on NetBeans it works.

However, when I build the JAR file and try to java -Djava.library.path="C:\\opencv\\build\\java\\x64" -jar myjar.jar from cmd, it throws

java.lang.NoClassDefFoundError: org/opencv/core/Mat

What am I missing to build my jar?

Edit

In the directory where .jar has been built, I create a dir called lib and inside I put opencv-300.jar: If I execute java -cp "lib/opencv-330.jar;myjar.jar" gui.Client it works... Is there any way to execute the app without running that command?

You can load a Native library file (.dll/Windows or .so/Linux) with two ways:

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);
    }

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