Example usage for java.lang System load

List of usage examples for java.lang System load

Introduction

In this page you can find the example usage for java.lang System load.

Prototype

@CallerSensitive
public static void load(String filename) 

Source Link

Document

Loads the native library specified by the filename argument.

Usage

From source file:com.alvermont.terraj.fracplanet.io.JarLibraryLoader.java

/**
 * Loads a native library. The native library is assumed
 * to be in a platform specific subdirectory of the lib directory in the jar. 
 * The library is writen to a local temporary directory and (hopefully) 
 * deleted at JVM exit.//from   w  w  w. j  a v a 2  s.  c  o m
 *
 * Dll extraction code taken from Gabriele Piero Nizzoli
 * http://www.nizzoli.net/index.php?itemid=15
 *
 * @param name The base name of the library to be loaded
 * @throws IOException if there is a problem loading the library
 */
public static void loadLibrary(String name) throws IOException {
    try {
        File libFile = extractLibrary(name);

        if (libFile != null) {
            log.debug("Calling System.load on: " + libFile.getPath());

            System.load(libFile.getPath());
        }
    } catch (IOException ioe) {
        log.error("IOException loading library", ioe);

        throw ioe;
    }
}

From source file:org.nd4j.linalg.api.buffer.util.LibUtils.java

/**
 * Load the library with the given name from a resource.
 * The extension for the current OS will be appended.
 *
 * @param libName The library name/*from   ww w  .ja v a 2  s.  co  m*/
 * @throws Throwable If the library could not be loaded
 */
private static void loadLibraryResource(String libName) throws Throwable {
    String libPrefix = createLibPrefix();
    String libExtension = createLibExtension();
    String fullName = libPrefix + libName;
    String resourceName = "/lib/" + fullName + "." + libExtension;
    InputStream inputStream = LibUtils.class.getResourceAsStream(resourceName);
    if (inputStream == null) {
        throw new NullPointerException("No resource found with name '" + resourceName + "'");
    }
    File tempFile = File.createTempFile(fullName, "." + libExtension);
    tempFile.deleteOnExit();
    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(tempFile);
        byte[] buffer = new byte[8192];
        while (true) {
            int read = inputStream.read(buffer);
            if (read < 0) {
                break;
            }
            outputStream.write(buffer, 0, read);
        }
        outputStream.flush();
        outputStream.close();
        outputStream = null;
        System.load(tempFile.toString());
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:com.jsonstore.util.JSONStoreUtil.java

/**
 * This method assumes it will find the library at:
 *     files/featurelibs/{arch}/{library}.zip
 *
 * It will unzip the library to the root folder
 * then see if any other architecture folders exist and delete them since
 * they will never be used on this architecture.
 *
 * @param ctx/*w ww.ja  va 2s.com*/
 * @param library example "libcrypto.so.1.0.0"
 *
 *
 */
public static final synchronized void loadLib(Context ctx, String library) {

    // keep track of which libs are already loaded, so we don't process multiple calls for the same lib unnecessarily
    // Notice we use a static.  This means calls to loadLib for the same 'library' parameter will be processed
    // only upon app startup, not app foreground.  We want to keep the behavior for cases where the native app has been
    // updated (through the Play Store, for example) and the target .so file needs to be replaced.

    if (!LOADED_LIBS.contains(library)) {

        // we only support "armeabi" and "x86"
        final String ARMEABI = "armeabi";
        final String X86 = "x86";

        String arch = System.getProperty("os.arch"); // the architecture we're running on
        String nonArch = null; // the architecture we are NOT on
        coreLogger.logDebug("os.arch: " + arch);
        if (arch != null && (arch.toLowerCase().startsWith("i") || arch.toLowerCase().startsWith("x86"))) { // i686, x86_64
            arch = X86;
            nonArch = ARMEABI;
        } else {
            arch = ARMEABI;
            nonArch = X86;
        }

        final String libPath = arch + File.separator + library;

        File nonArchStorage = new File(getNoBackupFilesDir(ctx), nonArch);

        deleteDirectory(nonArchStorage);

        File targetFile = new File(getNoBackupFilesDir(ctx), library);

        // delete the target
        targetFile.delete();

        coreLogger.logDebug("Extracting zip file: " + libPath);
        try {
            InputStream istr = ctx.getAssets().open(libPath + ".zip");
            unpack(istr, targetFile.getParentFile());
        } catch (IOException e) {
            e.printStackTrace();
            coreLogger.logDebug("Error extracting zip file: " + e.getMessage());
        }

        coreLogger.logDebug("Loading library using System.load: " + targetFile.getAbsolutePath());
        System.load(targetFile.getAbsolutePath());
    }

    LOADED_LIBS.add(library);
}

From source file:com.cloudera.recordservice.tests.MiniClusterController.java

/**
 * This method starts a minicluster with the specified number of nodes. This
 * method calls via JNI the cpp StartMiniCluster method which sleeps
 * indefinitely after starting the cluster, so this method will not return
 * unless the cluster is stopped./*from  www . j a  v a2s. c  o  m*/
 */
private static void startMiniCluster(int numNodes) {
    String rsHome = System.getenv("RECORD_SERVICE_HOME");
    if (rsHome == null) {
        throw new IllegalStateException("Required environment variable RECORD_SERVICE_HOME is not set.");
    }
    String path = rsHome + BUILT_RS_CODE_LOCATION;
    System.load(path + MINICLUSTER_LIBRARY);
    System.out.println("Number of nodes: " + numNodes);
    MiniClusterController.StartMiniCluster(numNodes);
}

From source file:org.apache.jk.apr.AprImpl.java

/** This method of loading the libs doesn't require setting
 *   LD_LIBRARY_PATH. Assuming a 'right' binary distribution,
 *   or a correct build all files will be in their right place.
 *
 *  The burden is on our code to deal with platform specific
 *  extensions and to keep the paths consistent - not easy, but
 *  worth it if it avoids one extra step for the user.
 *
 *  Of course, this can change to System.load() and putting the
 *  libs in LD_LIBRARY_PATH./*w  w w  .  j  av  a2  s .co  m*/
 */
public void loadNative() throws Throwable {
    if (aprHome == null)
        aprHome = baseDir;

    // XXX Update for windows
    if (jniMode) {
        /* In JNI mode we use mod_jk for the native functions.
           This seems the cleanest solution that works with multiple
           VMs.
        */
        if (jniModeSo.equals("inprocess")) {
            ok = true;
            return;
        }
        try {
            log.info("Loading " + jniModeSo);
            if (jniModeSo != null)
                System.load(jniModeSo);
        } catch (Throwable ex) {
            // ignore
            //ex.printStackTrace();
            return;
        }
        ok = true;
        return;
    }

    /*
      jkjni _must_ be linked with apr and crypt -
      this seem the only ( decent ) way to support JDK1.4 and
      JDK1.3 at the same time
      try {
          System.loadLibrary( "crypt" );
      } catch( Throwable ex ) {
          // ignore
          ex.printStackTrace();
      }
      try {
          System.loadLibrary( "apr" );
      } catch( Throwable ex ) {
          System.out.println("can't load apr, that's fine");
          ex.printStackTrace();
      }
    */
    try {
        if (nativeSo == null) {
            // This will load libjkjni.so or jkjni.dll in LD_LIBRARY_PATH
            log.debug("Loading jkjni from " + System.getProperty("java.library.path"));
            System.loadLibrary("jkjni");
        } else {
            System.load(nativeSo);
        }
    } catch (Throwable ex) {
        ok = false;
        //ex.printStackTrace();
        throw ex;
    }
}

From source file:org.apache.jk.apr.AprImpl.java

public void loadNative(String libPath) {
    try {//from  www.ja  va2 s . c  o m
        System.load(libPath);
    } catch (Throwable ex) {
        ok = false;
        if (log.isDebugEnabled())
            log.debug("Error loading native library ", ex);
    }
}

From source file:de.static_interface.sinklibrary.SinkLibrary.java

public void loadLibs(@Nullable SinkUser user) {
    if (user == null) {
        user = getConsoleUser();// w w w . j  av a2s  .  c o m
    }

    File[] files = LIB_FOLDER.listFiles();
    if (files != null) {
        for (File file : files) {
            try {
                boolean loaded = true;
                String path = file.getCanonicalPath();

                // Don't load the same libs again (e.g. when using /sreload)
                if (loadedLibs.contains(path)) {
                    continue;
                }

                if (file.getName().endsWith(".jar")) {
                    user.sendMessage(ChatColor.DARK_GREEN + "Loading jar library: " + file.getName());
                    addJarToClasspath(file.toURI().toURL());
                } else if (file.getName().endsWith(".class")) {
                    addClassToClasspath(path);
                    user.sendMessage(ChatColor.DARK_GREEN + "Loading class file: " + file.getName());
                } else if (file.getName().endsWith(".so") || file.getName().endsWith(".dll")) {
                    user.sendMessage(ChatColor.DARK_GREEN + "Loading native library: " + file.getName());
                    System.load(path);
                } else {
                    user.sendMessage(ChatColor.RED + "Warning! Skipped unknown file: " + file.getName());
                    loaded = false;
                }

                if (loaded) {
                    loadedLibs.add(path);
                }
            } catch (Throwable thr) {
                user.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED
                        + "An exception occurred while loading file: " + file.getName());
            }
        }
    }

    if (loadedLibs.size() > 0) {
        getLogger().info("Loaded " + loadedLibs.size() + " Libraries");
    }
}

From source file:org.apache.sysml.utils.NativeHelper.java

/**
 * Attempts to load native BLAS//from   w  w w.j a va  2  s.  c  o  m
 * 
 * @param customLibPath can be null (if we want to only want to use LD_LIBRARY_PATH), else the 
 * @param blas can be gomp, openblas or mkl_rt
 * @param optionalMsg message for debugging
 * @return true if successfully loaded BLAS
 */
private static boolean loadBLAS(String customLibPath, String blas, String optionalMsg) {
    // First attempt to load from custom library path
    if (customLibPath != null) {
        String libPath = customLibPath + File.separator + System.mapLibraryName(blas);
        try {
            System.load(libPath);
            // Print to stdout as this feature is intended for cloud environment
            System.out.println("Loaded the library:" + libPath);
            return true;
        } catch (UnsatisfiedLinkError e1) {
            // Print to stdout as this feature is intended for cloud environment
            System.out.println("Unable to load " + libPath + ":" + e1.getMessage());
        }
    }

    // Then try loading using loadLibrary
    try {
        System.loadLibrary(blas);
        return true;
    } catch (UnsatisfiedLinkError e) {
        if (optionalMsg != null)
            LOG.debug("Unable to load " + blas + "(" + optionalMsg + "):" + e.getMessage());
        else
            LOG.debug("Unable to load " + blas + ":" + e.getMessage());
        return false;
    }
}

From source file:com.izforge.izpack.util.Librarian.java

/**
 * Loads a library given its path.//from w w w . j  a v a2  s. c  om
 *
 * @param path   the library path
 * @param client the native library client
 * @return <tt>true</tt> if the library was loaded successfully, otherwise <tt>false</tt>
 */
private boolean load(String path, NativeLibraryClient client) {
    boolean result = false;
    try {
        System.load(path);
        clients.add(client);
        result = true;
    } catch (Throwable exception) {
        logger.log(Level.FINE, "Failed to load library: " + path + ": " + exception.getMessage(), exception);
    }
    return result;
}

From source file:org.apache.sysml.utils.NativeHelper.java

private static boolean loadLibraryHelper(String path) {
    OutputStream out = null;//from   w w  w .  j a v a2  s.c om
    try (InputStream in = NativeHelper.class.getResourceAsStream("/lib/" + path)) {
        // This logic is added because Java does not allow to load library from a resource file.
        if (in != null) {
            File temp = File.createTempFile(path, "");
            temp.deleteOnExit();
            out = FileUtils.openOutputStream(temp);
            IOUtils.copy(in, out);
            System.load(temp.getAbsolutePath());
            return true;
        } else
            LOG.warn("No lib available in the jar:" + path);
    } catch (IOException e) {
        LOG.warn("Unable to load library " + path + " from resource:" + e.getMessage());
    } finally {
        IOUtilFunctions.closeSilently(out);
    }
    return false;
}