1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 import java.io.FileOutputStream;
10 import java.io.InputStream;
11 import java.io.OutputStream;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
16 public final class NativeLib {
17 /* Statically load the library which contains all native functions used in here */
18 static private boolean isNativeInited = false;
19 public static void nativeInit() {
23 //if (System.getProperty("os.name").toLowerCase().startsWith("win"))
24 // NativeLib.nativeInit("winpthread-1");
26 NativeLib.nativeInit("simgrid");
27 NativeLib.nativeInit("surf-java");
28 NativeLib.nativeInit("simgrid-java");
29 isNativeInited = true;
32 public static void nativeInit(String name) {
34 /* Prefer the version of the library bundled into the jar file and use it */
36 } catch (SimGridLibNotFoundException e) {
37 /* If not found, try to see if we can find a version on disk */
39 System.loadLibrary(name);
40 } catch (UnsatisfiedLinkError e2) {
41 System.err.println("Cannot load the bindings to the "+name+" library in path "+getPath());
43 System.err.println("This jar file does not seem to fit your system, and I cannot find an installation of SimGrid.");
49 public static String getPath() {
50 String prefix = "NATIVE";
51 String os = System.getProperty("os.name");
52 String arch = System.getProperty("os.arch");
54 if (arch.matches("^i[3-6]86$"))
56 else if (arch.equalsIgnoreCase("x86_64"))
58 else if (arch.equalsIgnoreCase("AMD64"))
61 if (os.toLowerCase().startsWith("win")){
63 } else if (os.contains("OS X"))
66 os = os.replace(' ', '_');
67 arch = arch.replace(' ', '_');
69 return prefix + "/" + os + "/" + arch + "/";
71 static Path tempDir = null;
72 private static void loadLib (String name) throws SimGridLibNotFoundException {
73 String Path = NativeLib.getPath();
76 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
79 filename = "lib"+name+".so";
80 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
83 filename = name+".dll";
84 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
87 filename = "lib"+name+".dll";
88 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
91 filename = "lib"+name+".dylib";
92 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
95 throw new SimGridLibNotFoundException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
98 // We must write the lib onto the disk before loading it -- stupid operating systems
99 if (tempDir == null) {
100 tempDir = Files.createTempDirectory("simgrid-java");
101 // don't leak the files on disk, but remove it on JVM shutdown
102 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
104 File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
106 /* copy the library in position */
107 OutputStream out = new FileOutputStream(fileOut);
108 byte[] buffer = new byte[4096];
110 while ((bytes_read = in.read(buffer)) != -1) // Read until EOF
111 out.write(buffer, 0, bytes_read);
113 /* close all file descriptors, and load that shit */
116 System.load(fileOut.getAbsolutePath());
117 } catch (Exception e) {
118 System.err.println("Error while extracting the native library from the jar: ");
120 throw new SimGridLibNotFoundException("Cannot load the bindings to the "+name+" library in path "+getPath(), e);
124 /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
125 private static class FileCleaner implements Runnable {
127 public FileCleaner(File dir) {
132 for (File f : dir.listFiles())
135 } catch(Exception e) {
136 System.out.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");
143 public static void main(String[] args) {
144 System.out.println("This jarfile searches the native code under: " +getPath());
148 class SimGridLibNotFoundException extends Exception {
149 private static final long serialVersionUID = 1L;
150 public SimGridLibNotFoundException(String msg) {
154 public SimGridLibNotFoundException(String msg, Exception e) {