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 private static boolean isNativeInited = false;
20 static Path tempDir = null;
23 throw new IllegalAccessError("Utility class");
26 public static void nativeInit() {
30 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
31 NativeLib.nativeInit("winpthread-1");
33 NativeLib.nativeInit("simgrid");
34 NativeLib.nativeInit("simgrid-java");
35 isNativeInited = true;
38 public static void nativeInit(String name) {
40 /* Prefer the version of the library bundled into the jar file and use it */
42 } catch (LinkageException embeededException) {
43 /* If not found, try to see if we can find a version on disk */
45 System.loadLibrary(name);
46 } catch (UnsatisfiedLinkError systemException) {
47 if (! "boost_context".equals(name)) { // Ignore when we cannot load boost_context
49 System.err.println("\nCannot load the bindings to the "+name+" library in path "+getPath());
50 Throwable cause = embeededException.getCause();
51 if (cause instanceof java.lang.UnsatisfiedLinkError) {
52 if (cause.getMessage().matches(".*libcgraph.so.*"))
53 System.err.println("HINT: Try to install the libcgraph package (sudo apt-get install libcgraph).");
54 else if (cause.getMessage().matches(".*libboost_context.so.*"))
55 System.err.println("HINT: Try to install the boost-context package (sudo apt-get install libboost-context-dev).");
57 System.err.println("Try to install the missing dependencies, which name should appear above.");
59 System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk.");
62 cause.printStackTrace();
69 public static String getPath() {
70 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
71 String prefix = "NATIVE";
72 String os = System.getProperty("os.name");
73 String arch = System.getProperty("os.arch");
75 if (arch.matches("^i[3-6]86$"))
77 else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
80 if (os.toLowerCase().startsWith("win")){
82 } else if (os.contains("OS X"))
85 os = os.replace(' ', '_');
86 arch = arch.replace(' ', '_');
88 return prefix + "/" + os + "/" + arch + "/";
91 private static void loadLib (String name) throws LinkageException {
92 String path = NativeLib.getPath();
95 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
96 OutputStream out = null;
99 filename = "lib"+name+".so";
100 in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
103 filename = name+".dll";
104 in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
107 filename = "lib"+name+".dll";
108 in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
111 filename = "lib"+name+".dylib";
112 in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
115 throw new LinkageException("Cannot find library "+name+" in path "+path+". Sorry, but this jar does not seem to be usable on your machine.");
118 // We must write the lib onto the disk before loading it -- stupid operating systems
119 if (tempDir == null) {
120 tempDir = Files.createTempDirectory("simgrid-java-");
121 // don't leak the files on disk, but remove it on JVM shutdown
122 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
124 File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
126 /* copy the library in position */
127 out = new FileOutputStream(fileOut);
128 byte[] buffer = new byte[4096];
130 while ((bytesRead = in.read(buffer)) != -1) // Read until EOF
131 out.write(buffer, 0, bytesRead);
134 System.load(fileOut.getAbsolutePath());
135 } catch (SecurityException|UnsatisfiedLinkError|IOException e) {
136 throw new LinkageException("Cannot load the bindings to the "+name+" library in path "+getPath(), e);
138 /* Always close all descriptors, no matter success or error */
141 } catch (IOException e) {
142 /* Too bad. I dont care. */
148 } catch (IOException e) {
149 /* Too bad too. I dont care either. */
155 /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
156 private static class FileCleaner implements Runnable {
158 public FileCleaner(File dir) {
164 for (File f : dir.listFiles())
166 System.err.println("Unable to clean temporary file "+f.getAbsolutePath()+" during shutdown.");
168 System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");
169 } catch(Exception e) {
170 System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown: "+e.getCause());
177 public static void main(String[] args) {
178 System.out.println("This jarfile searches the native code under: " +getPath());
182 class LinkageException extends Exception {
183 private static final long serialVersionUID = 1L;
184 public LinkageException(String msg) {
188 public LinkageException(String msg, Throwable e) {