我使用 eclipse File-->New-->Other-->Plug-in Project
创建了一个 OSGi 插件( bundle ),名为插件 1。
我想在此插件中使用 native .so
库。我已在插件项目的根目录中添加了 libtest_app_wrap1.so
。
我的项目结构如下所示
这是 list 文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: JniTest
Bundle-SymbolicName: JniTest
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: jnitest.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-NativeCode: libtest_app_wrap1.so; osname=linux; processor=amd64
这是我的 Activator 类的代码
package jnitest;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
public Activator() {
System.loadLibrary("test_app_wrap1");
System.out.println("Library Loaded Successfully.......");
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
try {
test_app.foo();
} catch (Throwable tr) {
tr.printStackTrace();
}
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!!");
}
}
我使用 Eclipse 导出功能从插件项目创建 jar 文件。输出 jar 文件包含 .so
库。
当我调用System.loadLibrary("test_app_wrap1");
时,我没有收到任何异常或错误,但是当我调用方法test_app.foo();
时,我没有收到任何异常或错误。 code> 它给了我 UnsatisfiedLinkError
。 test_app.foo()
是一个 jni 方法,定义在 .so
中。
我没有得到任何解决此错误的线索。请帮忙解决。
[编辑:输出包内容]
这是输出jar的内容
最佳答案
通过以下方式将 jar 添加到项目中:项目->属性->库->添加 Jar 通过plugin.xml->Runtime->ClassPath部分->Add将jar添加到类路径中
或者 使用以下方式添加jar
java -Djava.library.path
并通过以下方式验证:
System.getProperty("java.library.path")
关于java - 从 OSGi bundle 调用 native 方法时出现 UnsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46236261/