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.IOException;
12 import java.io.OutputStream;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
17 public final class NativeLib {
18 /* Statically load the library which contains all native functions used in here */
19 static private boolean isNativeInited = false;
20 public static void nativeInit() {
24 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
25 NativeLib.nativeInit("winpthread-1");
27 NativeLib.nativeInit("simgrid");
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 (LinkageException embeededException) {
37 /* If not found, try to see if we can find a version on disk */
39 System.loadLibrary(name);
40 } catch (UnsatisfiedLinkError systemException) {
41 if (! name.equals("boost_context")) { // Ignore when we cannot load boost_context
43 System.err.println("\nCannot load the bindings to the "+name+" library in path "+getPath());
44 Throwable cause = embeededException.getCause();
45 if (cause instanceof java.lang.UnsatisfiedLinkError) {
46 if (cause.getMessage().matches(".*libcgraph.so.*"))
47 System.err.println("HINT: Try to install the libcgraph package (sudo apt-get install libcgraph).");
48 else if (cause.getMessage().matches(".*libboost_context.so.*"))
49 System.err.println("HINT: Try to install the boost-context package (sudo apt-get install libboost-context-dev).");
51 System.err.println("Try to install the missing dependencies, which name should appear above.");
53 System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk.");
56 cause.printStackTrace();
63 public static String getPath() {
64 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
65 String prefix = "NATIVE";
66 String os = System.getProperty("os.name");
67 String arch = System.getProperty("os.arch");
69 if (arch.matches("^i[3-6]86$"))
71 else if (arch.equalsIgnoreCase("x86_64"))
73 else if (arch.equalsIgnoreCase("AMD64"))
76 if (os.toLowerCase().startsWith("win")){
78 } else if (os.contains("OS X"))
81 os = os.replace(' ', '_');
82 arch = arch.replace(' ', '_');
84 return prefix + "/" + os + "/" + arch + "/";
86 static Path tempDir = null;
87 private static void loadLib (String name) throws LinkageException {
88 String Path = NativeLib.getPath();
91 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
94 filename = "lib"+name+".so";
95 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
98 filename = name+".dll";
99 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
102 filename = "lib"+name+".dll";
103 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
106 filename = "lib"+name+".dylib";
107 in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
110 throw new LinkageException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
113 // We must write the lib onto the disk before loading it -- stupid operating systems
114 if (tempDir == null) {
115 tempDir = Files.createTempDirectory("simgrid-java-");
116 // don't leak the files on disk, but remove it on JVM shutdown
117 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
119 File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
121 /* copy the library in position */
122 OutputStream out = new FileOutputStream(fileOut);
123 byte[] buffer = new byte[4096];
125 while ((bytes_read = in.read(buffer)) != -1) // Read until EOF
126 out.write(buffer, 0, bytes_read);
128 /* close all file descriptors, and load that shit */
131 System.load(fileOut.getAbsolutePath());
132 } catch (SecurityException|UnsatisfiedLinkError|IOException e) {
133 throw new LinkageException("Cannot load the bindings to the "+name+" library in path "+getPath(), e);
137 /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
138 private static class FileCleaner implements Runnable {
140 public FileCleaner(File dir) {
146 for (File f : dir.listFiles())
148 System.err.println("Unable to clean temporary file "+f.getAbsolutePath()+" during shutdown.");
150 System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");
151 } catch(Exception e) {
152 System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown: "+e.getCause());
159 public static void main(String[] args) {
160 System.out.println("This jarfile searches the native code under: " +getPath());
164 class LinkageException extends Exception {
165 private static final long serialVersionUID = 1L;
166 public LinkageException(String msg) {
170 public LinkageException(String msg, Throwable e) {