diff --git a/README.md b/README.md index 1b5e7dc..09b07dc 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,13 @@ -Battery -======= +# Battery Java 8+ JNI wrapper to the [starship battery](https://crates.io/crates/starship-battery) crate. -Usage ------ +## Usage The library can be installed from the `releases` repository on [maven.lostluma.net](https://maven.lostluma.net/#/releases/net/lostluma/battery) in two variants: - `default`: Downloads, validates, and caches the dynamic library on demand. Saves bandwidth and disk space. -- `bundled`: Contains the dynamic library for all platforms. Recommended for situations in which first startup may be offline. +- `bundled`: Contains the dynamic library for all platforms. Recommended if first application startup may be offline. The dynamic library is comes prebuilt for the following platforms: @@ -19,7 +17,9 @@ The dynamic library is comes prebuilt for the following platforms: | MacOS | yes | yes | | | Windows | yes | yes | | -Misc ----- +Running on other platforms is also possible, however some manual setup is required: +First build the project with Cargo, then set the `battery.natives.path` system property to the file path. + +## Misc This is my first time using both Rust and JNI :) diff --git a/src/main/java/net/lostluma/battery/impl/Constants.java b/src/main/java/net/lostluma/battery/impl/Constants.java index 38d20c4..8fdfe8a 100644 --- a/src/main/java/net/lostluma/battery/impl/Constants.java +++ b/src/main/java/net/lostluma/battery/impl/Constants.java @@ -11,6 +11,7 @@ public final class Constants { public static final String NATIVES_VERSION = "1.2.0"; public static final Path DEFAULT_CACHE_DIR = getDefaultCacheDir(); + public static final String NATIVES_PATH_PROPERTY = "battery.natives.path"; private static String getUserHome() { return System.getProperty("user.home"); diff --git a/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java b/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java index daeb42e..b7c06da 100644 --- a/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java +++ b/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java @@ -9,6 +9,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.Properties; @@ -44,6 +45,23 @@ public static void setAllowDownloads(boolean value) { } private static void load0() throws IOException, LibraryLoadError{ + Path path; + String custom = System.getProperty(Constants.NATIVES_PATH_PROPERTY); + + if (custom == null) { + path = getPath(); + } else { + path = Paths.get(custom); + } + + try { + System.load(path.toAbsolutePath().toString()); + } catch (UnsatisfiedLinkError e) { + throw new LibraryLoadError(e); + } + } + + private static Path getPath() throws IOException, LibraryLoadError { Properties properties = new Properties(); try (InputStream stream = NativeUtil.class.getResourceAsStream(METADATA)) { @@ -84,11 +102,7 @@ private static void load0() throws IOException, LibraryLoadError{ } } - try { - System.load(path.toAbsolutePath().toString()); - } catch (UnsatisfiedLinkError e) { - throw new LibraryLoadError(e); - } + return path; } private static String getArch() {