4

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

Somnath Kadam
  • 6,051
  • 6
  • 21
  • 37
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
  • 1
    Unless you've coded for JNI, you're probably not going to get much further even *after* you successfully load your .so. Please check out the Sun JNI tutorial: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html – paulsm4 Aug 02 '12 at 18:40

5 Answers5

10

Do the following:

  • Use System.loadLibrary("mylib");
  • Copy mylib.so to libmylib.so
  • Run java -Djava.library.path=/root/ Main
Reimeus
  • 158,255
  • 15
  • 216
  • 276
6

"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");
}
muru
  • 4,723
  • 1
  • 34
  • 78
Karol Król
  • 3,320
  • 1
  • 34
  • 37
1

You should add the so to library path: -Djava.libarary.path= (this is in the java command).

if you run from eclipse: How to add native library to "java.library.path" with Eclipse launch (instead of overriding it)

Community
  • 1
  • 1
Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
0

If you compiled opencv, on installation you should have seen something like:

make install:
-- Up-to-date: /usr/local/share/java/opencv4/libopencv_java460.so
-- Up-to-date: /usr/local/share/java/opencv4/opencv-460.jar

make a hard link to the /usr/lib/ folder:

$ sudo ln /usr/local/share/java/opencv4/libopencv_java460.so /usr/lib/libopencv_java460.so

And then just run:

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

that he will get the *.so

Gil
  • 43
  • 3
-7

As Reimeus answered. Or you can use System.load("/Library/Path/libsample.so");

twid
  • 6,368
  • 4
  • 32
  • 50