11package org .ldk .impl ;
22import org .ldk .enums .*;
3+ import java .io .File ;
4+ import java .io .InputStream ;
5+ import java .io .IOException ;
6+ import java .nio .file .Files ;
7+ import java .nio .file .Path ;
8+ import java .nio .file .StandardCopyOption ;
39
410public class bindings {
511 public static class VecOrSliceDef {
@@ -11,7 +17,24 @@ public VecOrSliceDef(long dataptr, long datalen, long stride) {
1117 }
1218 }
1319 static {
14- System .loadLibrary ("lightningjni" );
20+ try {
21+ // Try to load natively first, this works on Android and in testing.
22+ System .loadLibrary ("lightningjni" );
23+ } catch (UnsatisfiedLinkError _ignored ) {
24+ // Otherwise try to load from the library jar.
25+ File tmpdir = new File (System .getProperty ("java.io.tmpdir" ), "ldk-java-nativelib" );
26+ tmpdir .mkdir (); // If it fails to create, assume it was there already
27+ tmpdir .deleteOnExit ();
28+ String libname = "liblightningjni_" + System .getProperty ("os.name" ).replaceAll (" " , "" ) +
29+ "-" + System .getProperty ("os.arch" ).replaceAll (" " , "" ) + ".nativelib" ;
30+ try (InputStream is = bindings .class .getResourceAsStream ("/" + libname )) {
31+ Path libpath = new File (tmpdir .toPath ().toString (), "liblightningjni.so" ).toPath ();
32+ Files .copy (is , libpath , StandardCopyOption .REPLACE_EXISTING );
33+ Runtime .getRuntime ().load (libpath .toString ());
34+ } catch (IOException e ) {
35+ throw new IllegalArgumentException (e );
36+ }
37+ }
1538 init (java .lang .Enum .class , VecOrSliceDef .class );
1639 init_class_cache ();
1740 if (!get_lib_version_string ().equals (get_ldk_java_bindings_version ()))
@@ -25,7 +48,7 @@ public VecOrSliceDef(long dataptr, long datalen, long stride) {
2548 static native String get_lib_version_string ();
2649
2750 public static String get_ldk_java_bindings_version () {
28- return "v0.0.98.1 " ;
51+ return "v0.0.98.2 " ;
2952 }
3053 public static native String get_ldk_c_bindings_version ();
3154 public static native String get_ldk_version ();
0 commit comments