使用模块时无法加载 FFI 提供程序-info.java

Could not load FFI provider when using module-info.java

我的 Linux Java 应用程序使用蓝牙设备。我使用这个库来与 bluez stack 通信:https://github.com/hypfvieh/bluez-dbus.

在我将 module-info.java 添加到我的客户端代码之前,它工作得很好。突然我得到这个错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: could not load FFI provider jnr.ffi.provider.jffi.Provider at jnr.ffi@2.1.15/jnr.ffi.provider.InvalidRuntime.newLoadError(InvalidRuntime.java:101) at jnr.ffi@2.1.15/jnr.ffi.provider.InvalidRuntime.findType(InvalidRuntime.java:42) at jnr.ffi@2.1.15/jnr.ffi.Struct$NumberField.(Struct.java:872) at jnr.ffi@2.1.15/jnr.ffi.Struct$Unsigned16.(Struct.java:1240) at jnr.unixsocket@0.33/jnr.unixsocket.SockAddrUnix$DefaultSockAddrUnix.(SockAddrUnix.java:209) at jnr.unixsocket@0.33/jnr.unixsocket.SockAddrUnix.create(SockAddrUnix.java:174) at jnr.unixsocket@0.33/jnr.unixsocket.UnixSocketAddress.(UnixSocketAddress.java:53) at dbus.java@3.2.3/org.freedesktop.dbus.connections.transports.UnixSocketTransport.(UnixSocketTransport.java:32) at dbus.java@3.2.3/org.freedesktop.dbus.connections.transports.TransportFactory.createTransport(TransportFactory.java:37) at dbus.java@3.2.3/org.freedesktop.dbus.connections.AbstractConnection.(AbstractConnection.java:161) at dbus.java@3.2.3/org.freedesktop.dbus.connections.impl.DBusConnection.(DBusConnection.java:334) at dbus.java@3.2.3/org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:149) at dbus.java@3.2.3/org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:169) at dbus.java@3.2.3/org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:219) at dbus.java@3.2.3/org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:184) at bluez.dbus@0.1.3/com.github.hypfvieh.bluetooth.DeviceManager.createInstance(DeviceManager.java:74) at ...

Caused by: java.lang.UnsatisfiedLinkError: could not get native definition for type POINTER, original error message follows: java.lang.UnsatisfiedLinkError: could not locate stub library in jar file. Tried [jni/x86_64-Linux/libjffi-1.2.so, /jni/x86_64-Linux/libjffi-1.2.so] at jffi@1.2.23/com.kenai.jffi.internal.StubLoader.getStubLibraryStream(StubLoader.java:450) at jffi@1.2.23/com.kenai.jffi.internal.StubLoader.loadFromJar(StubLoader.java:375) at jffi@1.2.23/com.kenai.jffi.internal.StubLoader.load(StubLoader.java:278) at jffi@1.2.23/com.kenai.jffi.internal.StubLoader.(StubLoader.java:487) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:427) at jffi@1.2.23/com.kenai.jffi.Init.load(Init.java:68) at jffi@1.2.23/com.kenai.jffi.Foreign$InstanceHolder.getInstanceHolder(Foreign.java:49) at jffi@1.2.23/com.kenai.jffi.Foreign$InstanceHolder.(Foreign.java:45) at jffi@1.2.23/com.kenai.jffi.Foreign.getInstance(Foreign.java:103) at jffi@1.2.23/com.kenai.jffi.Type$Builtin.lookupTypeInfo(Type.java:242) at jffi@1.2.23/com.kenai.jffi.Type$Builtin.getTypeInfo(Type.java:237) at jffi@1.2.23/com.kenai.jffi.Type.resolveSize(Type.java:155) at jffi@1.2.23/com.kenai.jffi.Type.size(Type.java:138) at jnr.ffi@2.1.15/jnr.ffi.provider.jffi.NativeRuntime$TypeDelegate.size(NativeRuntime.java:178) at jnr.ffi@2.1.15/jnr.ffi.provider.AbstractRuntime.(AbstractRuntime.java:48) at jnr.ffi@2.1.15/jnr.ffi.provider.jffi.NativeRuntime.(NativeRuntime.java:57) at jnr.ffi@2.1.15/jnr.ffi.provider.jffi.NativeRuntime.(NativeRuntime.java:41) at jnr.ffi@2.1.15/jnr.ffi.provider.jffi.NativeRuntime$SingletonHolder.(NativeRuntime.java:53) at jnr.ffi@2.1.15/jnr.ffi.provider.jffi.NativeRuntime.getInstance(NativeRuntime.java:49) at jnr.ffi@2.1.15/jnr.ffi.provider.jffi.Provider.(Provider.java:29) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:124) at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:346) at java.base/java.lang.Class.newInstance(Class.java:604) at jnr.ffi@2.1.15/jnr.ffi.provider.FFIProvider$SystemProviderSingletonHolder.getInstance(FFIProvider.java:68) at jnr.ffi@2.1.15/jnr.ffi.provider.FFIProvider$SystemProviderSingletonHolder.(FFIProvider.java:57) at jnr.ffi@2.1.15/jnr.ffi.provider.FFIProvider.getSystemProvider(FFIProvider.java:35) at jnr.ffi@2.1.15/jnr.ffi.Runtime$SingletonHolder.(Runtime.java:82) at jnr.ffi@2.1.15/jnr.ffi.Runtime.getSystemRuntime(Runtime.java:67) at jnr.unixsocket@0.33/jnr.unixsocket.SockAddrUnix.(SockAddrUnix.java:46) at jnr.unixsocket@0.33/jnr.unixsocket.SockAddrUnix$DefaultSockAddrUnix.(SockAddrUnix.java:208) at jnr.unixsocket@0.33/jnr.unixsocket.SockAddrUnix.create(SockAddrUnix.java:174) at jnr.unixsocket@0.33/jnr.unixsocket.UnixSocketAddress.(UnixSocketAddress.java:53) at dbus.java@3.2.3/org.freedesktop.dbus.connections.transports.UnixSocketTransport.(UnixSocketTransport.java:32) at ...

我试图要求模块中与此错误相关的所有内容-info.java 但没有成功:

module org.example {
    requires dbus.java;
    requires dbus.java.nativefd;
    requires bluecove.linux.custom;
    requires bluez.dbus;
    requires jnr.ffi;
    requires jnr.x86asm;
    requires jnr.posix;
    requires jnr.enxio;
    requires jnr.unixsocket;
    requires jnr.constants;
    requires jnr.a64asm;
}

提前致谢。

TL;DR

这对我有用……

java --patch-module jffi={{your.own.location}}/com/github/jnr/jffi/1.3.0/jffi-1.3.0-native.jar …

长版

我下载了this example project。这不是 JPMS 项目。但是我在四个 sub-projects.

中的每一个中都添加了 module-info.java 文件

我将项目的 com.github.jnr:jnr-ffi 依赖项从 2.0.9 更新为 2.1.5 以匹配我在您的堆栈跟踪中看到的版本。我添加了以下依赖项…

…
<dependency>
    <groupId>com.github.hypfvieh</groupId>
    <artifactId>dbus-java</artifactId>
    <version>3.2.3</version>
</dependency>
<dependency>
    <groupId>com.github.hypfvieh</groupId> <artifactId>bluez-dbus</artifactId>
    <version>0.1.3</version>
</dependency>
<dependency>
    <groupId>com.rm5248</groupId>
    <artifactId>dbus-java-nativefd</artifactId>
    <version>1.0</version>
</dependency>
…

添加这些是为了让我可以将我的四个 module-info.java 描述符中的每一个定义为与您的描述符非常相似……

module get.pid{
    exports getpid;
    requires dbus.java;
    requires dbus.java.nativefd;
    requires bluez.dbus;
    requires jnr.ffi;
    requires jffi;
    requires jnr.x86asm;
    requires jnr.posix;
    requires jnr.enxio;
    requires jnr.unixsocket;
    requires jnr.constants;
    requires jnr.a64asm;
}

在我执行 --patch-module 之前,我 运行 一个示例项目得到了与你完全相同的错误。

但是使用以下命令,所有示例应用程序 运行 完全可以作为模块...

java --patch-module jffi={{your.own.location}}/com/github/jnr/jffi/1.3.0/jffi-1.3.0-native.jar --add-modules org.objectweb.asm --add-exports org.objectweb.asm/org.objectweb.asm=jnr.ffi --add-exports org.objectweb.asm/org.objectweb.asm.signature=jnr.ffi --module-path {{path.to.all.the.jars}} -m get.pid/getpid.Getpid

add-modulesadd-exports也很重要。所以不要遗漏这些。