in native/src/main/java/org/mvndaemon/mvnd/nativ/MvndNativeLoader.java [161:215]
private static boolean extractAndLoadLibraryFile(
String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
// Include architecture name in temporary filename in order to avoid conflicts
// when multiple JVMs with different architectures running at the same time
String uuid = randomUUID();
String extractedLibFileName = String.format("mvndnative-%s-%s-%s", getVersion(), uuid, libraryFileName);
String extractedLckFileName = extractedLibFileName + ".lck";
File extractedLibFile = new File(targetFolder, extractedLibFileName);
File extractedLckFile = new File(targetFolder, extractedLckFileName);
try {
// Extract a native library file into the target directory
try (InputStream in = MvndNativeLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
if (!extractedLckFile.exists()) {
new FileOutputStream(extractedLckFile).close();
}
try (OutputStream out = new FileOutputStream(extractedLibFile)) {
copy(in, out);
}
} finally {
// Delete the extracted lib file on JVM exit.
extractedLibFile.deleteOnExit();
extractedLckFile.deleteOnExit();
}
// Set executable (x) flag to enable Java to load the native library
extractedLibFile.setReadable(true);
extractedLibFile.setWritable(true);
extractedLibFile.setExecutable(true);
// Check whether the contents are properly copied from the resource folder
try (InputStream nativeIn = MvndNativeLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
try (InputStream extractedLibIn = new FileInputStream(extractedLibFile)) {
String eq = contentsEquals(nativeIn, extractedLibIn);
if (eq != null) {
throw new RuntimeException(String.format(
"Failed to write a native library file at %s because %s", extractedLibFile, eq));
}
}
}
// Load library
if (loadNativeLibrary(extractedLibFile)) {
nativeLibrarySourceUrl = MvndNativeLoader.class
.getResource(nativeLibraryFilePath)
.toExternalForm();
return true;
}
} catch (IOException e) {
System.err.println(e.getMessage());
}
return false;
}