diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java index e4ba4ce2a7..d9244314fb 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java @@ -53,14 +53,16 @@ import static io.netty.channel.unix.Errors.newIOException; */ public final class Native { static { - String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim(); - if (!name.startsWith("linux")) { - throw new IllegalStateException("Only supported on Linux"); + try { + // First, try calling a side-effect free JNI method to see if the library was already + // loaded by the application. + offsetofEpollData(); + } catch (UnsatisfiedLinkError ignore) { + // The library was not previously loaded, load it now. + loadNativeLibrary(); } - NativeLibraryLoader.load(SystemPropertyUtil.get("io.netty.packagePrefix", "").replace('.', '-') + - "netty-transport-native-epoll", - PlatformDependent.getClassLoader(Native.class)); } + // EventLoop operations and constants public static final int EPOLLIN = epollin(); public static final int EPOLLOUT = epollout(); @@ -250,4 +252,13 @@ public final class Native { private Native() { // utility } + + private static void loadNativeLibrary() { + String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim(); + if (!name.startsWith("linux")) { + throw new IllegalStateException("Only supported on Linux"); + } + NativeLibraryLoader.load(SystemPropertyUtil.get("io.netty.packagePrefix", "").replace('.', '-') + + "netty-transport-native-epoll", PlatformDependent.getClassLoader(Native.class)); + } }