From 1a551809fdb95d43a5cf202b8c495f07ace30308 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 13 Sep 2024 07:31:31 -0400 Subject: [PATCH] fix loading native libraries from build directory --- .vscode/launch.json | 12 ++++++++++++ src/main/java/monero/common/MoneroUtils.java | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..1002984d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "configurations": [ + { + "type": "java", + "name": "Launch Current File", + "request": "launch", + "mainClass": "${file}", + "projectName": "monero-java", + "vmArgs": "-Djava.library.path=./build", + } + ] +} \ No newline at end of file diff --git a/src/main/java/monero/common/MoneroUtils.java b/src/main/java/monero/common/MoneroUtils.java index a782a486..8465fb38 100644 --- a/src/main/java/monero/common/MoneroUtils.java +++ b/src/main/java/monero/common/MoneroUtils.java @@ -60,6 +60,7 @@ public static void loadNativeLibrary() { try { String libName = (System.getProperty("os.name").toLowerCase().contains("windows") ? "lib" : "") + "monero-java"; System.loadLibrary(libName); + if (isNativeLibraryLoaded()) return; } catch (Exception | UnsatisfiedLinkError e) { // ignore error } @@ -93,13 +94,14 @@ public static void loadNativeLibrary() { libraryCppFile = "libmonero-cpp.dylib"; libraryJavaFile = "libmonero-java.dylib"; } else { - throw new UnsupportedOperationException("Unsupported operating system: " + osName); + throw new MoneroError("Unsupported operating system: " + osName); } // copy all library files to temp directory tempDir = Files.createTempDirectory("libmonero"); for (String libraryFile : libraryFiles) { try (InputStream inputStream = MoneroUtils.class.getResourceAsStream(libraryPath + libraryFile); OutputStream outputStream = Files.newOutputStream(tempDir.resolve(libraryFile))) { + if (inputStream == null) throw new MoneroError("Missing native library for monero-java: " + libraryFile); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) {