Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load and use native library in java?

Tags:

java

native

I've got a java class, calling a native method and trying to load library:

import java.io.UnsupportedEncodingException;

public class Main {

    public static native String getMyString(String s);

    /**
     * @param args
     * @throws UnsupportedEncodingException
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        // System.out.println("here!");

        String s2 = getMyString("string text");
        for (Byte b : s2.getBytes("UTF-8")) {
            System.out.print(b);
            System.out.print(",");
        }

    }

    static {
        System.loadLibrary("mylib.so");
    }

}

The "mylib.so" is in the directory, where Main.class is located.

When I run java Main I get following exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no mylib.so in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1856)
        at java.lang.Runtime.loadLibrary0(Runtime.java:845)
        at java.lang.System.loadLibrary(System.java:1084)
        at Main.<clinit>(Main.java:24)

What should I change for this to wark?

I've tried setting library full path without success

like image 487
Arsen Zahray Avatar asked Feb 19 '23 16:02

Arsen Zahray


2 Answers

Do the following:

  • Use System.loadLibrary("mylib");
  • Copy mylib.so to libmylib.so
  • Run java -Djava.library.path=/root/ Main
like image 196
Reimeus Avatar answered Mar 05 '23 15:03

Reimeus


"How to load native library"

public final class NativeLibsLoaderUtil {
    private static final String JAVA_LIBRARY_PATH = "java.library.path";
    private static final String SYS_PATHS = "sys_paths";

    private NativeLibsLoaderUtil() {
    }

    private static void addLibsToJavaLibraryPath(final String tmpDirName) {
        try {
            System.setProperty(JAVA_LIBRARY_PATH, tmpDirName);
            /* Optionally add these two lines */
            System.setProperty("jna.library.path", tmpDirName);
            System.setProperty("jni.library.path", tmpDirName);
            final Field fieldSysPath = ClassLoader.class.getDeclaredField(SYS_PATHS);
            fieldSysPath.setAccessible(true);
            fieldSysPath.set(null, null);
        } catch (IllegalAccessException | NoSuchFieldException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }   
}

Where tmpDirName is a directory where you store your library. Or you can modify above class and use temp directory from your system property, like this:

/**
 * Temporary directory system property name
 */
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";

/**
 *
 * @return
 */
private static File getTempDir() {
    final String tmpDirName = System.getProperty(JAVA_IO_TMPDIR);
    final File tmpDir = new File(tmpDirName);
    if (!tmpDir.exists()) {
        tmpDir.mkdir();
    }
    return tmpDir;
}

!But first you have to copy there your native lib :)

Then to load native library call "addLibsToJavaLibraryPath" method in static block in "most root" class before any class constructor was executed.

static {
    NativeLibsLoaderUtil.addLibsToJavaLibraryPath("/tmp");
}
like image 26
Karol Król Avatar answered Mar 05 '23 15:03

Karol Król