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("simgrid-java");
28 isNativeInited = true;
31 public static void nativeInit(String name) {
33 /* Prefer the version of the library bundled into the jar file and use it */
35 } catch (SimGridLibNotFoundException embeededException) {
36 /* If not found, try to see if we can find a version on disk */
38 System.loadLibrary(name);
39 } catch (UnsatisfiedLinkError systemException) {
40 if (! name.equals("boost_context")) { // Ignore when we cannot load boost_context
42 System.err.println("\nCannot load the bindings to the "+name+" library in path "+getPath());
43 Throwable cause = embeededException.getCause();
44 if (cause instanceof java.lang.UnsatisfiedLinkError && cause.getMessage().matches(".*libcgraph.so.*"))
45 System.err.println("HINT: Try to install the libcgraph package (sudo apt-get install libcgraph).");
47 System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk.");
49 cause.printStackTrace();
56 public static String getPath() {
57 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
58 String prefix = "NATIVE";
59 String os = System.getProperty("os.name");
60 String arch = System.getProperty("os.arch");
62 if (arch.matches("^i[3-6]86$"))
64 else if (arch.equalsIgnoreCase("x86_64"))
66 else if (arch.equalsIgnoreCase("AMD64"))
69 if (os.toLowerCase().startsWith("win")){
71 } else if (os.contains("OS X"))
74 os = os.replace(' ', '_');
75 arch = arch.replace(' ', '_');
77 return prefix + "/" + os + "/" + arch + "/";
79 static Path tempDir = null;
80 private static void loadLib (String name) throws SimGridLibNotFoundException {
81 String Path = NativeLib.getPath();
84 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
87 filename = "lib"+name+".so";
88 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
91 filename = name+".dll";
92 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
95 filename = "lib"+name+".dll";
96 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
99 filename = "lib"+name+".dylib";
100 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
103 throw new SimGridLibNotFoundException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
106 // We must write the lib onto the disk before loading it -- stupid operating systems
107 if (tempDir == null) {
108 tempDir = Files.createTempDirectory("simgrid-java");
109 // don't leak the files on disk, but remove it on JVM shutdown
110 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
112 File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
114 /* copy the library in position */
115 OutputStream out = new FileOutputStream(fileOut);
116 byte[] buffer = new byte[4096];
118 while ((bytes_read = in.read(buffer)) != -1) // Read until EOF
119 out.write(buffer, 0, bytes_read);
121 /* close all file descriptors, and load that shit */
124 System.load(fileOut.getAbsolutePath());
125 } catch (Throwable e) {
126 throw new SimGridLibNotFoundException("Cannot load the bindings to the "+name+" library in path "+getPath(), e);
130 /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
131 private static class FileCleaner implements Runnable {
133 public FileCleaner(File dir) {
139 for (File f : dir.listFiles())
142 } catch(Exception e) {
143 System.out.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");
150 public static void main(String[] args) {
151 System.out.println("This jarfile searches the native code under: " +getPath());
155 class SimGridLibNotFoundException extends Exception {
156 private static final long serialVersionUID = 1L;
157 public SimGridLibNotFoundException(String msg) {
161 public SimGridLibNotFoundException(String msg, Throwable e) {