NIH, or (or Native Integration Helper) is a tiny library for Java 22 and above that helps with loading native libraries. Based on code from JNA, it deals with bundling platform specific libraries along with your application jars, and overcoming some problems with locating such libraries when using the new FFMAPI available in modern Java.
The library is available in Maven Central, so configure your project according to the build system you use. For example, for Maven itself :-
<dependencies>
<dependency>
<groupId>com.sshtools</groupId>
<artifactId>nih</artifactId>
<version>0.9.0</version>
</dependency>
</dependencies>
Development builds may be available in the snapshots repository
<repositories>
<repository>
<id>oss-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
..
<dependencies>
<dependency>
<groupId>com.sshtools</groupId>
<artifactId>dobbin</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
</dependencies>
Simply use Native.load()
methods instead of FFMAPIs default methods to obtain a SymbolLookup
. This deals with several platform specific problems that as of writing, the base Java tools do not deal with (Linux library paths with symbolic links, and Mac OS frameworks do not seem to be handled well).
For example, Native.load("c", Arena.ofAuto());
will locate that standard C library.
First, include compiled shared libraries as a Java resource (e.g. src/main/resources
) in the sub-path META-INF/shared-libraries/<os>/<arch>
.
Then to load the library in Java and obtain a SymbolLookup
, use Native.load()
methods.
For example, say you are working on 64-bit Linux and had a native shared library compiled from tray.c
. Your native Makefile
produces a libtray.so
.
You would place libtray.so
into src/main/resource/META-INF/shared-libraries/linux/x86-64
, and then use Native.load("tray", Arena.ofAuto())
to obtain the library handle.
If you then want to add Windows support, you would then place your tray.dll
into src/main/resource/META-INF/shared-libraries/windows/x86-64
.
The 2 classes in this library originated from JNA, but were adapted to be useful with Java's own FFMAPI. This code, and our minor additions to it are licensed under LGPL 2.1 or later and Apache License 2.0. (starting with JNA version 4.0.0).