UnsatisfiedLinkError when using JNI?(使用 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?
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01