简体   繁体   中英

Unable to load .so in JNI project

I've read through a number of similar questions but continue to be baffled by this project. I'm trying a very simple JNI project using Eclipse on Ubuntu 11.04 64-bit.

I've built my .so file correctly. I know this because I am able to call it when I use System.load("/exact/path/to/my/libcproject.so") from my Java program. But when I attempt to use System.loadLibrary("libcproject") I get an unsatisfied link error.

Here's my simple Java program:

package pkg;
public class MyClass {
static {
    System.out.println("java.library.path: " + System.getProperty("java.library.path"));
    System.loadLibrary("libcproject");
}
private native String foo(String input);
public static void main(String[] args) {
    MyClass mc = new MyClass();
    String output = mc.foo("input");
    System.out.println(output);
}
}

As you can see, I'm printing what the java.library.path is. When I run the program, I get:

java.library.path: /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/amd64/server:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/amd64:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/../lib/amd64:/home/mlevin/workspace/cproject/Debug:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
input

"input" is what my C function returns (it just gives you back what you gave it).

If I change the System.load call to System.loadLibrary("libcproject"), I get the unsatisfied link error, even though, as you can see from the output, /home/mlevin/workspace/cproject/Debug, which is where libcproject.so lives, is in the java.library.path.

java.library.path: /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/amd64/server:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/amd64:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/../lib/amd64:/home/mlevin/workspace/cproject/Debug:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Exception in thread "main" java.lang.UnsatisfiedLinkError: no libcproject in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at pkg.MyClass.<clinit>(MyClass.java:7)
Could not find the main class: pkg.MyClass.  Program will exit.

I'm pretty baffled at this point. Any suggestions?

Thanks

Drop the lib prefix, like this:

System.loadLibrary("cproject");

Java automatically adds lib at the start and .so at the end (for Unix-like OSes, anyway. On Windows it adds .dll to the end).

.export LD_LIBRARY_PATH =。,从而设置当前目录的库路径,以便找到Java文件。

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