Example usage for java.lang System mapLibraryName

List of usage examples for java.lang System mapLibraryName

Introduction

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

Prototype

public static native String mapLibraryName(String libname);

Source Link

Document

Maps a library name into a platform-specific string representing a native library.

Usage

From source file:Main.java

public static void main(String[] args) {

    String str = System.mapLibraryName("os.name");
    System.out.println(str);
}

From source file:org.caleydo.core.view.opengl.layout2.internal.SandBoxLibraryLoader.java

/**
 * extract the given library of the classpath and put it to a temporary file
 *//*from   w  w w .j a va  2s.  co m*/
public static File toTemporaryFile(String libName) throws IOException {
    // convert to native library name
    libName = System.mapLibraryName(libName);
    if (SystemUtils.IS_OS_MAC_OSX)
        libName = StringUtils.replace(libName, ".dylib", ".jnilib");

    // create
    String extension = Files.getFileExtension(libName);
    File file = File.createTempFile(StringUtils.removeEnd(libName, extension), "." + extension);
    file.deleteOnExit();

    URL res = SandBoxLibraryLoader.class.getResource("/" + libName);
    if (res == null)
        throw new FileNotFoundException("can't extract: " + libName);
    try (InputStream in = res.openStream();
            OutputStream to = new BufferedOutputStream(new FileOutputStream(file))) {
        ByteStreams.copy(in, to);
    } catch (IOException e) {
        System.err.println("can't extract: " + libName);
        e.printStackTrace();
        throw new FileNotFoundException("can't extract: " + libName);
    }
    return file;
}

From source file:kyotocabinet.Loader.java

/**
 * Load the native library./*from ww  w  . jav  a  2  s.  c  om*/
 */
static synchronized void load() {
    if (loaded) {
        return;
    }
    String lib = System.mapLibraryName("jkyotocabinet");
    if (lib.endsWith(".dll")) {
        // lib?lib
        URL url = Loader.class.getClassLoader().getResource(System.mapLibraryName("jkyotocabinet"));
        String libFileName = "./temp" + File.separatorChar + System.mapLibraryName("jkyotocabinet");
        File file = new File(libFileName);
        try {
            FileUtils.copyURLToFile(url, file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.load(file.getAbsolutePath());
    } else {
        System.loadLibrary("jkyotocabinet");
    }
    loaded = true;
}

From source file:org.mule.module.launcher.nativelib.AbstractNativeLibraryFinderTestCase.java

protected File createDefaultNativeLibraryFile(String libName) throws IOException {
    return createNativeLibraryFile(libFolder.getRoot(), System.mapLibraryName(libName));
}

From source file:org.dmlc.xgboost4j.util.Initializer.java

/**
 * load native library, this method will first try to load library from java.library.path, then try to load library in jar package.
 * @param libName/*from  w ww  .ja  va2 s .c  o m*/
 * @throws IOException 
 */
private static void smartLoad(String libName) throws IOException {
    addNativeDir(nativePath);
    try {
        System.loadLibrary(libName);
    } catch (UnsatisfiedLinkError e) {
        try {
            NativeUtils.loadLibraryFromJar(nativeResourcePath + System.mapLibraryName(libName));
        } catch (IOException e1) {
            throw e1;
        }
    }
}

From source file:org.cryptomator.jni.JniModule.java

@Provides
@Singleton//w w  w .  j  a va2  s.  c o  m
public Optional<MacFunctions> macFunctions(Lazy<MacFunctions> macFunctions) {
    if (SystemUtils.IS_OS_MAC_OSX) {
        try {
            System.loadLibrary(MacFunctions.LIB_NAME);
            LOG.info("loaded {}", System.mapLibraryName(MacFunctions.LIB_NAME));
            return Optional.of(macFunctions.get());
        } catch (UnsatisfiedLinkError e) {
            LOG.error("Could not load JNI lib {} from path {}", System.mapLibraryName(MacFunctions.LIB_NAME),
                    System.getProperty("java.library.path"));
        }
    }
    return Optional.empty();
}

From source file:org.mule.module.launcher.nativelib.AbstractNativeLibraryFinderTestCase.java

protected String getJniLibFileName() {
    String libraryFileName = System.mapLibraryName(TEST_LIB_NAME);
    int index = libraryFileName.lastIndexOf(".");
    libraryFileName = libraryFileName.substring(0, index) + PerAppNativeLibraryFinder.JNILIB_EXTENSION;
    return libraryFileName;
}

From source file:org.esa.s2tbx.dataio.NativeLibraryLoader.java

/**
 * Loads library either from the current JAR archive, or from file system
 * <p>/*from www  .ja v  a 2 s .co  m*/
 * The file from JAR is copied into system temporary directory and then loaded.
 *
 * @param path          The path from which the load is attempted
 * @param libraryName   The name of the library to be loaded (without extension)
 * @throws IOException              If temporary file creation or read/write operation fails
 */
public static void loadLibrary(String path, String libraryName) throws IOException {
    path = URLDecoder.decode(path, "UTF-8");
    String libPath = "/lib/" + NativeLibraryLoader.getOSFamily() + "/" + System.mapLibraryName(libraryName);
    if (path.contains(".jar!")) {
        try (InputStream in = NativeLibraryLoader.class.getResourceAsStream(libPath)) {
            File fileOut = new File(System.getProperty("java.io.tmpdir"), libPath);
            try (OutputStream out = FileUtils.openOutputStream(fileOut)) {
                IOUtils.copy(in, out);
            }
            path = fileOut.getAbsolutePath();
        }
    } else {
        path = new File(path, libPath).getAbsolutePath();
    }
    System.load(path);
}

From source file:org.gradle.logging.internal.TerminalDetector.java

public TerminalDetector(File libCacheDir) {
    // Some hackery to prevent JNA from creating a shared lib in the tmp dir, as it does not clean things up
    File tmpDir = new File(libCacheDir, "jna");
    tmpDir.mkdirs();/*from  www.  j  av a 2s. c  o m*/
    String libName = OperatingSystem.current() instanceof OperatingSystem.MacOs ? "libjnidispatch.jnilib"
            : System.mapLibraryName("jnidispatch");
    File libFile = new File(tmpDir, libName);
    if (!libFile.exists()) {
        String resourceName = "/com/sun/jna/" + OperatingSystem.current().getNativePrefix() + "/" + libName;
        try {
            InputStream lib = getClass().getResourceAsStream(resourceName);
            if (lib == null) {
                throw new GradleException(
                        String.format("Could not locate JNA native lib resource '%s'.", resourceName));
            }
            try {
                FileOutputStream outputStream = new FileOutputStream(libFile);
                try {
                    IOUtils.copy(lib, outputStream);
                } finally {
                    outputStream.close();
                }
            } finally {
                lib.close();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    System.setProperty("jna.boot.library.path", tmpDir.getAbsolutePath());
}

From source file:hivemall.xgboost.NativeLibLoader.java

private static void tryLoadNativeLibFromResource(final String libName) {
    // Resolve the library file name with a suffix (e.g., dll, .so, etc.)
    String resolvedLibName = System.mapLibraryName(libName);

    if (!hasResource(libPath + resolvedLibName)) {
        if (!getOSName().equals("Mac")) {
            return;
        }//  www  .  ja  v  a 2  s .  c  om
        // Fix for openjdk7 for Mac
        // A detail of this workaround can be found in https://github.com/xerial/snappy-java/issues/6
        resolvedLibName = "lib" + libName + ".jnilib";
        if (hasResource(libPath + resolvedLibName)) {
            return;
        }
    }
    try {
        File tempFile = createTempFileFromResource(resolvedLibName,
                NativeLibLoader.class.getResourceAsStream(libPath + resolvedLibName));
        logger.info("Copyed the native library in JAR as " + tempFile.getAbsolutePath());
        addLibraryPath(tempFile.getParent());
    } catch (Exception e) {
        // Simply ignore it here
        logger.info(e.getMessage());
    }
}