Skip to content

Commit 9fde7e5

Browse files
committed
NativeUtils: changed a way to collect java.library.path. Added extended info on UnsatisfiedLinkError
1 parent 8b6eef9 commit 9fde7e5

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/main/java/org/julia/jni/NativeUtils.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
*/
2424
package org.julia.jni;
2525

26-
import java.io.*;
26+
import java.io.File;
27+
import java.io.FileNotFoundException;
28+
import java.io.IOException;
29+
import java.io.InputStream;
2730
import java.lang.reflect.Field;
28-
import java.nio.file.FileSystemNotFoundException;
29-
import java.nio.file.FileSystems;
30-
import java.nio.file.Files;
31-
import java.nio.file.ProviderNotFoundException;
32-
import java.nio.file.StandardCopyOption;
31+
import java.nio.file.*;
32+
import java.util.ArrayList;
3333
import java.util.Collections;
3434
import java.util.List;
35+
import java.util.stream.Collectors;
3536

3637
/**
3738
* A simple library class which helps with loading dynamic libraries stored in the
@@ -76,7 +77,8 @@ private NativeUtils() {
7677
* (restriction of {@link File#createTempFile(java.lang.String, java.lang.String)}).
7778
* @throws FileNotFoundException If the file could not be found inside the JAR.
7879
*/
79-
public static void loadLibraryFromJar(String path) throws IOException {
80+
public static void loadLibraryFromJar(String path, String libraryPaths) throws IOException {
81+
setupJavaLibraryPaths(libraryPaths);
8082

8183
if (null == path || !path.startsWith("/")) {
8284
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
@@ -109,9 +111,12 @@ public static void loadLibraryFromJar(String path) throws IOException {
109111
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
110112
}
111113

112-
checkJavaLibraryPath();
113114
try {
114115
System.load(temp.getAbsolutePath());
116+
} catch (UnsatisfiedLinkError e) {
117+
throw new UnsatisfiedLinkError(
118+
String.format("%s\njava.library.path=%s\n",
119+
e.getMessage(), System.getProperty(JAVA_LIBRARY_PATH)));
115120
} finally {
116121
if (isPosixCompliant()) {
117122
// Assume POSIX compliant file system, can be deleted after loading
@@ -123,6 +128,10 @@ public static void loadLibraryFromJar(String path) throws IOException {
123128
}
124129
}
125130

131+
public static void loadLibraryFromJar(String path) throws IOException {
132+
loadLibraryFromJar(path, "");
133+
}
134+
126135
private static boolean isPosixCompliant() {
127136
try {
128137
return FileSystems.getDefault()
@@ -185,15 +194,23 @@ public static List<String> loadedLibraryNames() {
185194
return Collections.emptyList();
186195
}
187196

188-
private static void checkJavaLibraryPath() {
197+
private static void setupJavaLibraryPaths(String externalPaths) {
189198
String javaLibraryPath = System.getProperty(JAVA_LIBRARY_PATH);
190199
String javaHome = System.getProperty("java.home");
191-
if (!javaLibraryPath.contains(javaHome)) {
200+
// if (!javaLibraryPath.contains(javaHome)) {
201+
String javaLibPath = javaHome + File.separator + "lib";
202+
final List<String> newSysPaths =
203+
new ArrayList<>(List.of(javaLibraryPath.split(File.pathSeparator)));
204+
newSysPaths.add(javaLibPath);
205+
newSysPaths.add(javaLibPath + File.separator + "server");
206+
newSysPaths.addAll(List.of(externalPaths.split(File.pathSeparator)));
207+
192208
System.setProperty(JAVA_LIBRARY_PATH,
193-
javaLibraryPath + File.pathSeparator
194-
+ javaHome + File.separator + "lib"
209+
newSysPaths.stream()
210+
.filter(s -> !s.isEmpty())
211+
.distinct()
212+
.collect(Collectors.joining(File.pathSeparator))
195213
);
196-
}
197-
System.out.println(System.getProperty(JAVA_LIBRARY_PATH));
214+
// }
198215
}
199216
}

0 commit comments

Comments
 (0)