+ /** Try to extract the library from the jarfile before loading it */
+ private static boolean loadLibAsStream (String name) throws IOException, UnsatisfiedLinkError {
+ String path = NativeLib.getPath();
+
+ // We must write the lib onto the disk before loading it -- stupid operating systems
+ if (tempDir == null) {
+ tempDir = Files.createTempDirectory("simgrid-java-");
+ // don't leak the files on disk, but remove it on JVM shutdown
+ Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
+ }
+
+ /* For each possible filename of the given library on all possible OSes, try it */
+ for (String filename : new String[]
+ { name,
+ "lib"+name+".so", /* linux */
+ name+".dll", "lib"+name+".dll", /* windows (pure and mingw) */
+ "lib"+name+".dylib" /* mac osx */}) {
+
+ File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
+ boolean done = false;
+ try ( // Try-with-resources. These stream will be autoclosed when needed.
+ InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
+ OutputStream out = new FileOutputStream(fileOut);
+ ) {
+ if (in != null) {
+ /* copy the library in position */
+ byte[] buffer = new byte[4096];
+ int bytesRead;
+ while ((bytesRead = in.read(buffer)) != -1) // Read until EOF
+ out.write(buffer, 0, bytesRead);
+ done = true;
+ }
+ }
+ if (done) {
+ /* load that library */
+ System.load(fileOut.getAbsolutePath());
+
+ /* It loaded! we're good */
+ return true;
+ }
+ }
+
+ /* No suitable name found */
+ return false;
+ }
+
+ /** Find where to search for the library in the jar -- keep it aligned with where cmake puts it! */
+ private static String getPath() {
+ // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
+ String prefix = "NATIVE";
+ String os = System.getProperty("os.name");
+ String arch = System.getProperty("os.arch");
+
+ if (arch.matches("^i[3-6]86$"))
+ arch = "x86";
+ else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
+ arch = "amd64";