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

1

5 Answers 5

12

Do the following:

  • Use System.loadLibrary("mylib");
  • Copy mylib.so to libmylib.so
  • Run java -Djava.library.path=/root/ Main
Sign up to request clarification or add additional context in comments.

3 Comments

Also use -Djava.library.path=path-to-lib-file when starting java.
I made the changes and run the program with java Main -Djava.library.path=/root/, without any success
Note that the path passed to -Djava.library.path must be an absolute path.
7

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

Comments

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)

Comments

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

Comments

-7

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

1 Comment

... which makes your answer redundant.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.