使用 JNI 时出现 UnsatisfiedLinkError?

UnsatisfiedLinkError when using JNI?(使用 JNI 时出现 UnsatisfiedLinkError?)

本文介绍了使用 JNI 时出现 UnsatisfiedLinkError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 linux ubuntu 中使用 JNI 从 Java 程序调用 C 程序.

I want to call a C program from Java program using JNI in linux ubuntu.

我是新手,我已经尝试过 http://www.ibm.com/developerworks/java/tutorials/j-jni/section2.html.我已经创建了 .java、.h、.c 和 .so 文件.但是当我尝试运行该程序时,出现以下错误.

I am new to this and I have tried the sample program given in http://www.ibm.com/developerworks/java/tutorials/j-jni/section2.html . I have already created the .java, .h , .c and .so files. But when i tried to run the program I am getting the following error.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no Sample1 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 Sample1.main(Sample1.java:13)

推荐答案

我刚刚尝试让相同的示例在我的 CentOS 上运行,并得到了和你一样的错误.正如已经回答的那样,JVM 找不到所需的 so 文件.我使用 gcc 按照以下步骤成功使其工作:

I've just tried to get the same sample to work on my CentOS and got the same error as you. As already answered, JVM failed to find the so file needed. I succeeded to get it to work by following the steps below using gcc:

$ javac Sample1.java
$ javah Sample1
$ # Include paths must also be specified using -I option in the following gcc command line!
$ gcc -shared -I...snip... Sample1.c -o libSample1.so
$ # Library path for libSample1.so must also be specified!
$ java -Djava.library.path=...path/to/libSample1.so... Sample1

如果省略共享库的lib"前缀,JVM 会因为某种原因找不到它.我不知道为什么.我不熟悉 Linux 中共享库的命名约定.

If you omit the "lib" prefix of the shared library, JVM fails to find it for some reason. I don't know why. I am not familiar with the naming convention of shared libraries in Linux.

希望这篇文章能帮到你.

I hope this post could help.

这篇关于使用 JNI 时出现 UnsatisfiedLinkError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 JNI 时出现 UnsatisfiedLinkError?

基础教程推荐