From fe857f6156c319c9c424e457e9529973d0de308d Mon Sep 17 00:00:00 2001 From: TheTripleV Date: Tue, 30 Dec 2025 18:02:03 -0500 Subject: [PATCH 1/2] Try loading Swift libraries from JAR resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modified library loading mechanism to attempt loading from JAR resources before falling back to system library paths. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- .../FFM/FFMSwift2JavaGenerator.swift | 87 +++++++++++++++++-- .../swift/swiftkit/core/SwiftLibraries.java | 43 +++++++-- .../org/swift/swiftkit/ffm/SwiftRuntime.java | 17 +++- 3 files changed, 127 insertions(+), 20 deletions(-) diff --git a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift index a16ac8a89..e28de0627 100644 --- a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift +++ b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift @@ -194,10 +194,46 @@ extension FFMSwift2JavaGenerator { @SuppressWarnings("unused") private static final boolean INITIALIZED_LIBS = initializeLibs(); static boolean initializeLibs() { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); - System.loadLibrary(LIB_NAME); + // Load swiftCore: try path, then JAR, then /usr/lib/swift on macOS + try { + System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); + } catch (Throwable e) { + try { + SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); + } catch (Throwable e2) { + if (PlatformUtils.isMacOS()) { + try { + System.load("/usr/lib/swift/libswiftCore.dylib"); + } catch (Throwable e3) { + throw new RuntimeException("Failed to load swiftCore from java.library.path, JAR, and /usr/lib/swift", e3); + } + } else { + throw e2; + } + } + } + + // Load SwiftJava: try path, then JAR + try { + System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); + } catch (Throwable e) { + SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); + } + + // Load SwiftRuntimeFunctions: try path, then JAR + try { + System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + } catch (Throwable e) { + SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + } + + // Load module library: try path, then JAR + try { + System.loadLibrary(LIB_NAME); + } catch (Throwable e) { + SwiftLibraries.loadResourceLibrary(LIB_NAME); + } + return true; } @@ -345,10 +381,45 @@ extension FFMSwift2JavaGenerator { static final SymbolLookup SYMBOL_LOOKUP = getSymbolLookup(); private static SymbolLookup getSymbolLookup() { if (SwiftLibraries.AUTO_LOAD_LIBS) { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); - System.loadLibrary(LIB_NAME); + // Load swiftCore: try path, then JAR, then /usr/lib/swift on macOS + try { + System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); + } catch (Throwable e) { + try { + SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); + } catch (Throwable e2) { + if (PlatformUtils.isMacOS()) { + try { + System.load("/usr/lib/swift/libswiftCore.dylib"); + } catch (Throwable e3) { + throw new RuntimeException("Failed to load swiftCore from java.library.path, JAR, and /usr/lib/swift", e); + } + } else { + throw e2; + } + } + } + + // Load SwiftJava: try path, then JAR + try { + System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); + } catch (Throwable e) { + SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); + } + + // Load SwiftRuntimeFunctions: try path, then JAR + try { + System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + } catch (Throwable e) { + SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + } + + // Load module library: try path, then JAR + try { + System.loadLibrary(LIB_NAME); + } catch (Throwable e) { + SwiftLibraries.loadResourceLibrary(LIB_NAME); + } } if (PlatformUtils.isMacOS()) { diff --git a/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java b/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java index 2a138a408..0b89b5f2a 100644 --- a/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java +++ b/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java @@ -41,21 +41,41 @@ public final class SwiftLibraries { : Boolean.getBoolean("swiftkit.auto-load-libraries"); @SuppressWarnings("unused") - private static final boolean INITIALIZED_LIBS = loadLibraries(false); + private static final boolean INITIALIZED_LIBS = AUTO_LOAD_LIBS ? loadLibraries(false) : true; public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) { - System.loadLibrary(LIB_NAME_SWIFT_CORE); - System.loadLibrary(LIB_NAME_SWIFT_JAVA); - if (loadSwiftRuntimeFunctions) { - System.loadLibrary(LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + try { + System.loadLibrary(LIB_NAME_SWIFT_CORE); + System.loadLibrary(LIB_NAME_SWIFT_JAVA); + if (loadSwiftRuntimeFunctions) { + System.loadLibrary(LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + } + return true; + } catch (UnsatisfiedLinkError e) { + // Libraries not on path - will be loaded later from JAR or other location + if (CallTraces.TRACE_DOWNCALLS) { + System.err.println("[swift-java] Could not auto-load libraries from java.library.path: " + e.getMessage()); + System.err.println("[swift-java] Libraries will need to be loaded explicitly or from JAR resources"); + } + return false; } - return true; } // ==== ------------------------------------------------------------------------------------------------------------ // Loading libraries - public static void loadResourceLibrary(String libname) { + // Cache of already-loaded libraries to prevent duplicate extraction + private static final java.util.Map loadedLibraries = new java.util.HashMap<>(); + + public static synchronized void loadResourceLibrary(String libname) { + // Check if already loaded + if (loadedLibraries.containsKey(libname)) { + if (CallTraces.TRACE_DOWNCALLS) { + System.out.println("[swift-java] Library already loaded from cache: " + libname); + } + return; + } + String resourceName = PlatformUtils.dynamicLibraryName(libname); if (CallTraces.TRACE_DOWNCALLS) { System.out.println("[swift-java] Loading resource library: " + resourceName); @@ -66,12 +86,19 @@ public static void loadResourceLibrary(String libname) { throw new RuntimeException("Expected library '" + libname + "' ('" + resourceName + "') was not found as resource!"); } - // TODO: we could do an in memory file system here + // Extract to temp file File tempFile = File.createTempFile(libname, ""); tempFile.deleteOnExit(); Files.copy(libInputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); System.load(tempFile.getAbsolutePath()); + + // Cache the loaded library + loadedLibraries.put(libname, tempFile); + + if (CallTraces.TRACE_DOWNCALLS) { + System.out.println("[swift-java] Loaded and cached library: " + libname + " from " + tempFile.getAbsolutePath()); + } } catch (IOException e) { throw new RuntimeException("Failed to load dynamic library '" + libname + "' ('" + resourceName + "') as resource!", e); } diff --git a/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java b/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java index 74a270c0a..9ea9167ed 100644 --- a/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java +++ b/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java @@ -43,11 +43,20 @@ public class SwiftRuntime { private static final boolean INITIALIZED_LIBS = loadLibraries(false); public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) { - System.loadLibrary(STDLIB_DYLIB_NAME); - if (loadSwiftRuntimeFunctions) { - System.loadLibrary(SWIFT_RUNTIME_FUNCTIONS_DYLIB_NAME); + try { + System.loadLibrary(STDLIB_DYLIB_NAME); + if (loadSwiftRuntimeFunctions) { + System.loadLibrary(SWIFT_RUNTIME_FUNCTIONS_DYLIB_NAME); + } + return true; + } catch (UnsatisfiedLinkError e) { + // Libraries not on path - will be loaded later from JAR or other location + if (CallTraces.TRACE_DOWNCALLS) { + System.err.println("[swift-java] SwiftRuntime: Could not auto-load libraries from java.library.path: " + e.getMessage()); + System.err.println("[swift-java] Libraries will need to be loaded explicitly or from JAR resources"); + } + return false; } - return true; } static final SymbolLookup SYMBOL_LOOKUP = getSymbolLookup(); From 7a2e90164fb1fac66c41ab83470255b787398575 Mon Sep 17 00:00:00 2001 From: TheTripleV Date: Wed, 7 Jan 2026 00:16:00 -0800 Subject: [PATCH 2/2] refactor to address review comments --- .../FFM/FFMSwift2JavaGenerator.swift | 87 ++---------- .../swift/swiftkit/core/SwiftLibraries.java | 130 ++++++++++++++---- .../org/swift/swiftkit/ffm/SwiftRuntime.java | 18 +-- 3 files changed, 116 insertions(+), 119 deletions(-) diff --git a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift index e28de0627..ae047284c 100644 --- a/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift +++ b/Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift @@ -194,46 +194,10 @@ extension FFMSwift2JavaGenerator { @SuppressWarnings("unused") private static final boolean INITIALIZED_LIBS = initializeLibs(); static boolean initializeLibs() { - // Load swiftCore: try path, then JAR, then /usr/lib/swift on macOS - try { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); - } catch (Throwable e) { - try { - SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); - } catch (Throwable e2) { - if (PlatformUtils.isMacOS()) { - try { - System.load("/usr/lib/swift/libswiftCore.dylib"); - } catch (Throwable e3) { - throw new RuntimeException("Failed to load swiftCore from java.library.path, JAR, and /usr/lib/swift", e3); - } - } else { - throw e2; - } - } - } - - // Load SwiftJava: try path, then JAR - try { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); - } catch (Throwable e) { - SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); - } - - // Load SwiftRuntimeFunctions: try path, then JAR - try { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); - } catch (Throwable e) { - SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); - } - - // Load module library: try path, then JAR - try { - System.loadLibrary(LIB_NAME); - } catch (Throwable e) { - SwiftLibraries.loadResourceLibrary(LIB_NAME); - } - + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_CORE); + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_JAVA); + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + SwiftLibraries.loadLibraryWithFallbacks(LIB_NAME); return true; } @@ -381,45 +345,10 @@ extension FFMSwift2JavaGenerator { static final SymbolLookup SYMBOL_LOOKUP = getSymbolLookup(); private static SymbolLookup getSymbolLookup() { if (SwiftLibraries.AUTO_LOAD_LIBS) { - // Load swiftCore: try path, then JAR, then /usr/lib/swift on macOS - try { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); - } catch (Throwable e) { - try { - SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE); - } catch (Throwable e2) { - if (PlatformUtils.isMacOS()) { - try { - System.load("/usr/lib/swift/libswiftCore.dylib"); - } catch (Throwable e3) { - throw new RuntimeException("Failed to load swiftCore from java.library.path, JAR, and /usr/lib/swift", e); - } - } else { - throw e2; - } - } - } - - // Load SwiftJava: try path, then JAR - try { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); - } catch (Throwable e) { - SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA); - } - - // Load SwiftRuntimeFunctions: try path, then JAR - try { - System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); - } catch (Throwable e) { - SwiftLibraries.loadResourceLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); - } - - // Load module library: try path, then JAR - try { - System.loadLibrary(LIB_NAME); - } catch (Throwable e) { - SwiftLibraries.loadResourceLibrary(LIB_NAME); - } + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_CORE); + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_JAVA); + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + SwiftLibraries.loadLibraryWithFallbacks(LIB_NAME); } if (PlatformUtils.isMacOS()) { diff --git a/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java b/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java index 0b89b5f2a..b20437e8c 100644 --- a/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java +++ b/SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java @@ -45,16 +45,16 @@ public final class SwiftLibraries { public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) { try { - System.loadLibrary(LIB_NAME_SWIFT_CORE); - System.loadLibrary(LIB_NAME_SWIFT_JAVA); + loadLibraryWithFallbacks(LIB_NAME_SWIFT_CORE); + loadLibraryWithFallbacks(LIB_NAME_SWIFT_JAVA); if (loadSwiftRuntimeFunctions) { - System.loadLibrary(LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); + loadLibraryWithFallbacks(LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); } return true; - } catch (UnsatisfiedLinkError e) { - // Libraries not on path - will be loaded later from JAR or other location + } catch (RuntimeException e) { + // Libraries could not be loaded if (CallTraces.TRACE_DOWNCALLS) { - System.err.println("[swift-java] Could not auto-load libraries from java.library.path: " + e.getMessage()); + System.err.println("[swift-java] Could not load libraries: " + e.getMessage()); System.err.println("[swift-java] Libraries will need to be loaded explicitly or from JAR resources"); } return false; @@ -64,43 +64,115 @@ public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) { // ==== ------------------------------------------------------------------------------------------------------------ // Loading libraries - // Cache of already-loaded libraries to prevent duplicate extraction - private static final java.util.Map loadedLibraries = new java.util.HashMap<>(); + /** + * Returns the platform-specific system path to the swiftCore library. + * + * @return Full path to swiftCore on the system, or null if platform is not macOS or Linux + */ + public static String libSwiftCorePath() { + if (PlatformUtils.isMacOS()) { + return "/usr/lib/swift/libswiftCore.dylib"; + } else if (PlatformUtils.isLinux()) { + return "/usr/lib/swift/linux/libswiftCore.so"; + } + return null; + } - public static synchronized void loadResourceLibrary(String libname) { - // Check if already loaded - if (loadedLibraries.containsKey(libname)) { + /** + * Attempts to load a library using multiple fallback strategies with nice error reporting. + * Tries in order: java.library.path, JAR resources, system path (for swiftCore only). + * + * @param libname The library name to load + * @throws RuntimeException if all loading strategies fail + */ + public static void loadLibraryWithFallbacks(String libname) { + // Try 1: Load from java.library.path + try { + System.loadLibrary(libname); if (CallTraces.TRACE_DOWNCALLS) { - System.out.println("[swift-java] Library already loaded from cache: " + libname); + System.out.println("[swift-java] Loaded " + libname + " from java.library.path"); } return; + } catch (Throwable e) { + if (CallTraces.TRACE_DOWNCALLS) { + System.err.println("[swift-java] Failed to load " + libname + " from java.library.path: " + e.getMessage()); + } } - String resourceName = PlatformUtils.dynamicLibraryName(libname); - if (CallTraces.TRACE_DOWNCALLS) { - System.out.println("[swift-java] Loading resource library: " + resourceName); + // Try 2: Load from JAR resources + try { + loadResourceLibrary(libname); + if (CallTraces.TRACE_DOWNCALLS) { + System.out.println("[swift-java] Loaded " + libname + " from JAR resources"); + } + return; + } catch (Throwable e2) { + if (CallTraces.TRACE_DOWNCALLS) { + System.err.println("[swift-java] Failed to load " + libname + " from JAR: " + e2.getMessage()); + } + + // Try 3: For swiftCore only, try system path + if (libname.equals(LIB_NAME_SWIFT_CORE)) { + String systemPath = libSwiftCorePath(); + if (systemPath != null) { + try { + System.load(systemPath); + if (CallTraces.TRACE_DOWNCALLS) { + System.out.println("[swift-java] Loaded " + libname + " from system path: " + systemPath); + } + return; + } catch (Throwable e3) { + throw new RuntimeException( + "Failed to load " + libname + " from java.library.path, JAR resources, and system path (" + systemPath + ")", + e3 + ); + } + } else { + if (CallTraces.TRACE_DOWNCALLS) { + System.err.println("[swift-java] System path not available on this platform"); + } + } + } + + throw new RuntimeException("Failed to load " + libname + " from java.library.path and JAR resources", e2); } + } - try (InputStream libInputStream = SwiftLibraries.class.getResourceAsStream("/" + resourceName)) { - if (libInputStream == null) { - throw new RuntimeException("Expected library '" + libname + "' ('" + resourceName + "') was not found as resource!"); + // Cache of already-loaded libraries to prevent duplicate extraction + private static final java.util.concurrent.ConcurrentHashMap loadedLibraries = new java.util.concurrent.ConcurrentHashMap<>(); + + public static void loadResourceLibrary(String libname) { + loadedLibraries.computeIfAbsent(libname, key -> { + String resourceName = PlatformUtils.dynamicLibraryName(key); + if (CallTraces.TRACE_DOWNCALLS) { + System.out.println("[swift-java] Loading resource library: " + resourceName); } - // Extract to temp file - File tempFile = File.createTempFile(libname, ""); - tempFile.deleteOnExit(); - Files.copy(libInputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + try (InputStream libInputStream = SwiftLibraries.class.getResourceAsStream("/" + resourceName)) { + if (libInputStream == null) { + throw new RuntimeException("Expected library '" + key + "' ('" + resourceName + "') was not found as resource!"); + } - System.load(tempFile.getAbsolutePath()); + // TODO: we could do an in memory file system here + // Extract to temp file + File tempFile = File.createTempFile(key, ""); + tempFile.deleteOnExit(); + Files.copy(libInputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - // Cache the loaded library - loadedLibraries.put(libname, tempFile); + System.load(tempFile.getAbsolutePath()); - if (CallTraces.TRACE_DOWNCALLS) { - System.out.println("[swift-java] Loaded and cached library: " + libname + " from " + tempFile.getAbsolutePath()); + if (CallTraces.TRACE_DOWNCALLS) { + System.out.println("[swift-java] Loaded and cached library: " + key + " from " + tempFile.getAbsolutePath()); + } + + return tempFile; + } catch (IOException e) { + throw new RuntimeException("Failed to load dynamic library '" + key + "' ('" + resourceName + "') as resource!", e); } - } catch (IOException e) { - throw new RuntimeException("Failed to load dynamic library '" + libname + "' ('" + resourceName + "') as resource!", e); + }); + + if (CallTraces.TRACE_DOWNCALLS) { + System.out.println("[swift-java] Library already loaded from cache: " + libname); } } diff --git a/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java b/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java index 9ea9167ed..961f2324b 100644 --- a/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java +++ b/SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftRuntime.java @@ -16,6 +16,7 @@ import org.swift.swiftkit.core.SwiftInstance; import org.swift.swiftkit.core.CallTraces; +import org.swift.swiftkit.core.SwiftLibraries; import org.swift.swiftkit.core.util.PlatformUtils; import org.swift.swiftkit.ffm.SwiftRuntime.swiftjava; @@ -32,11 +33,6 @@ public class SwiftRuntime { - public static final String STDLIB_DYLIB_NAME = "swiftCore"; - public static final String SWIFT_RUNTIME_FUNCTIONS_DYLIB_NAME = "SwiftRuntimeFunctions"; - - private static final String STDLIB_MACOS_DYLIB_PATH = "/usr/lib/swift/libswiftCore.dylib"; - private static final Arena LIBRARY_ARENA = Arena.ofAuto(); @SuppressWarnings("unused") @@ -44,15 +40,15 @@ public class SwiftRuntime { public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) { try { - System.loadLibrary(STDLIB_DYLIB_NAME); + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_CORE); if (loadSwiftRuntimeFunctions) { - System.loadLibrary(SWIFT_RUNTIME_FUNCTIONS_DYLIB_NAME); + SwiftLibraries.loadLibraryWithFallbacks(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); } return true; - } catch (UnsatisfiedLinkError e) { - // Libraries not on path - will be loaded later from JAR or other location + } catch (RuntimeException e) { + // Libraries could not be loaded if (CallTraces.TRACE_DOWNCALLS) { - System.err.println("[swift-java] SwiftRuntime: Could not auto-load libraries from java.library.path: " + e.getMessage()); + System.err.println("[swift-java] SwiftRuntime: Could not load libraries: " + e.getMessage()); System.err.println("[swift-java] Libraries will need to be loaded explicitly or from JAR resources"); } return false; @@ -64,7 +60,7 @@ public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) { private static SymbolLookup getSymbolLookup() { if (PlatformUtils.isMacOS()) { // On Apple platforms we need to lookup using the complete path - return SymbolLookup.libraryLookup(STDLIB_MACOS_DYLIB_PATH, LIBRARY_ARENA) + return SymbolLookup.libraryLookup(SwiftLibraries.libSwiftCorePath(), LIBRARY_ARENA) .or(SymbolLookup.loaderLookup()) .or(Linker.nativeLinker().defaultLookup()); } else {