Java native utilities.
How to know what platform are running?
How to load shared library form JAR resource?
The Natives library can help you!
Import Natives classes:
import com.github.xuges.natives.*;
Check os, arch and platform:
System.out.println("current os: " + OS.CURRENT);
System.out.println("current arch: " + Arch.CURRENT);
System.out.println("current platform: " + Platform.CURRENT);
if (OS.CURRENT.isUnixLike())
System.out.println("unix-like");
else if (OS.CURRENT == OS.Windows)
System.out.println("windows");
Load native shared library from JAR:
try {
//will be:
//load !jar/org/example/libs/windows_x64/example.dll on Windows x64
//load !jar/org/example/libs/linux_aarch64/libexample.so on Linux aarch64
//load !jar/org/example/libs/macos_x64/libexample.dylib on MacOSX x64
//tips: load path format: org/example/libs/Platform.CURRENT
LibraryLoader.loadFromJar("org/example/libs", "example");
} catch (UnsatisfiedLinkError e) {
System.out.println("load library error: " + e);
}
Store native shared library to JAR:
#linux
$> ./natives-add example.jar org/example/libs linux_x64 libtest.so
#windows
$> natives-add example.jar org/example/libs windows_x64 test.dll
Why use this library?
- Multi-Platform: Windows Linux MacOSX BSD, x86 x64 aarch64 mips ppc loongarch64 and more.
- Easy-To-Use: Simple API (like System.loadLibrary) and tool scripts.
- Production-Ready: Automatic clean temporary file and production validated.
MIT License
Copyright(C) 2023 [email protected]