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:com.mcreations.usb.windows.JavaxUsb.java

/** Load native library */
private static void loadLibrary() throws UsbException {
    if (libraryLoaded)
        return;//from w  w w.  j a  v a 2s . c  om

    try {
        log(LOG_HOTPLUG, FUNC, CLASS, "loadLibrary", "Getting java.library.path");
        //            String temp = "java.library.path="+System.getProperty("java.library.path");

        //            System.out.println("java.library.path="+System.getProperty("java.library.path"));    
        System.loadLibrary(LIBRARY_NAME);
    } catch (Exception e) {
        throw new UsbException(EXCEPTION_WHILE_LOADING_SHARED_LIBRARY + " <"
                + System.mapLibraryName(LIBRARY_NAME) + "> : \n" + e.getMessage());
    } catch (Error e) {
        throw new UsbException(ERROR_WHILE_LOADING_SHARED_LIBRARY + " <" + System.mapLibraryName(LIBRARY_NAME)
                + "> : \n" + e.getMessage());
    }
}

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

/**
 * Attempts to load native BLAS/*w  ww .  j a v  a 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:SearchlistClassLoader.java

/**
 *  return the pathname to the specified native library.
 *
 *  If the library is not found on the searchpath, then <i>null</i>
 *  is returned, indicating to the Java machine that it should search
 *  java.library.path./*from ww  w. ja v  a 2  s.  c  o m*/
 *
 *  @param libname - the String name of the library to find
 *  @return the full path to the found library file, or <i>null</i>.
 */
public String findLibrary(String libname) {
    String fileName = System.mapLibraryName(libname);
    System.out.println("findLibrary: looking in " + this + " for " + libname + " as " + fileName);

    int i, j;
    URL[] url;
    File dir, file;
    Loader ldr;
    for (i = 0; (ldr = getLoader(i++, searchMode)) != null; i++) {
        if (ldr.loader instanceof SearchlistClassLoader)
            url = ((SearchlistClassLoader) ldr.loader).getSearchPath();
        else if (ldr.loader instanceof URLClassLoader)
            url = ((URLClassLoader) ldr.loader).getURLs();
        else
            url = null;

        if (url != null) {
            for (j = 0; j < url.length; j++) {
                if (!url[j].getProtocol().equalsIgnoreCase("file"))
                    continue;

                try {
                    dir = new File(url[j].toURI()).getParentFile();
                    file = new File(dir, fileName);

                    if (file.exists()) {
                        System.out.println("found: " + file.getAbsolutePath());

                        return file.getAbsolutePath();
                    }
                } catch (Exception e) {
                    System.out.println("Ignoring url: " + url[j] + ": " + e);
                }
            }
        }
    }

    // nothing found, use java.library.path
    return null;
}

From source file:com.imagine.BaseActivity.java

String mainSOPath() {
    String libname = "main";
    return libDir() + "/" + System.mapLibraryName(libname);
}

From source file:com.microsoft.tfs.jni.loader.NativeLoader.java

/**
 * Loads a native library from a directory.
 *
 * @param libraryName/* w ww .  j av  a  2  s.c o m*/
 *        the library name (short name, not including extension or "lib"
 *        prefix). Not null or empty.
 * @param directory
 *        the path to the directory (not null).
 * @param osgiOperatingSystem
 *        the OSGI-style operating system name to match resources on (not
 *        null or empty).
 * @param osgiArchitecture
 *        the OSGI-style architecture name. May be null or empty to omit the
 *        architecture string from the resource path during the search. Mac
 *        OS X uses "fat" shared libraries that support multiple
 *        architectures, and would omit this parameter.
 * @throws UnsatisfiedLinkError
 *         if a library that maps to the given library name cannot be found.
 */
private static void loadLibraryFromDirectory(final String libraryName, final String directory,
        final String osgiOperatingSystem, final String osgiArchitecture) {
    Check.notNullOrEmpty(libraryName, "libraryName"); //$NON-NLS-1$
    Check.notNull(directory, "directory"); //$NON-NLS-1$
    Check.notNull(osgiOperatingSystem, "osgiOperatingSystem"); //$NON-NLS-1$

    final File libraryDirectory = new File(directory);

    if (libraryDirectory.exists() == false) {
        throw new UnsatisfiedLinkError(MessageFormat.format("Native library base directory {0} does not exist", //$NON-NLS-1$
                libraryDirectory.getAbsolutePath()));
    }

    if (libraryDirectory.isDirectory() == false) {
        throw new UnsatisfiedLinkError(
                MessageFormat.format("Native library base directory {0} is not a directory", //$NON-NLS-1$
                        libraryDirectory.getAbsolutePath()));
    }

    /*
     * Compute the subdirectory (i.e. "linux/ppc") inside the base
     * directory.
     */

    File fullDirectory = new File(libraryDirectory, osgiOperatingSystem);

    if (osgiArchitecture != null && osgiArchitecture.length() > 0) {
        fullDirectory = new File(fullDirectory, osgiArchitecture);
    }

    /*
     * Convert "fun" into "libfun.so" or "fun.dll" or whatever, depending on
     * platform.
     *
     * Oracle broke mapLibraryName on Java 7 so that it returns
     * libname.dylib instead of libname.jnilib. (System.loadLibrary
     * continues to use .jnilib, however, so this wasn't a change of the
     * expected extension, it's just broken.) So we need to transform names
     * ourselves on Mac OS.
     */
    final String mappedLibraryName;

    if (Platform.isCurrentPlatform(Platform.MAC_OS_X)) {
        mappedLibraryName = "lib" + libraryName + ".jnilib"; //$NON-NLS-1$ //$NON-NLS-2$
    } else {
        mappedLibraryName = System.mapLibraryName(libraryName);
    }

    /*
     * Construct the full path.
     */

    final File fullLibraryFile = new File(fullDirectory, mappedLibraryName);

    if (fullLibraryFile.exists() == false) {
        throw new UnsatisfiedLinkError(MessageFormat.format("Native library file {0} does not exist", //$NON-NLS-1$
                fullLibraryFile.getAbsolutePath()));
    }

    if (fullLibraryFile.isDirectory() == true) {
        throw new UnsatisfiedLinkError(MessageFormat.format("Native library file {0} is a directory", //$NON-NLS-1$
                fullLibraryFile.getAbsolutePath()));
    }

    log.debug(
            MessageFormat.format("Trying to load native library file {0}", fullLibraryFile.getAbsolutePath())); //$NON-NLS-1$

    System.load(fullLibraryFile.getAbsolutePath());
    log.debug(MessageFormat.format("Loaded {0} from user-specified directory", //$NON-NLS-1$
            fullLibraryFile.getAbsolutePath()));

    return;
}

From source file:org.java.plugin.standard.StandardPluginClassLoader.java

/**
 * @see java.lang.ClassLoader#findLibrary(java.lang.String)
 *//*w w  w .  j ava  2s  .c  o  m*/
protected String findLibrary(final String name) {
    if ((name == null) || "".equals(name.trim())) { //$NON-NLS-1$
        return null;
    }
    if (log.isDebugEnabled()) {
        log.debug("findLibrary(String): name=" + name //$NON-NLS-1$
                + ", this=" + this); //$NON-NLS-1$
    }
    String libname = System.mapLibraryName(name);
    String result = null;
    PathResolver pathResolver = getPluginManager().getPathResolver();
    for (Iterator it = getPluginDescriptor().getLibraries().iterator(); it.hasNext();) {
        Library lib = (Library) it.next();
        if (lib.isCodeLibrary()) {
            continue;
        }
        URL libUrl = pathResolver.resolvePath(lib, lib.getPath() + libname);
        if (log.isDebugEnabled()) {
            log.debug("findLibrary(String): trying URL " + libUrl); //$NON-NLS-1$
        }
        File libFile = IoUtil.url2file(libUrl);
        if (libFile != null) {
            if (log.isDebugEnabled()) {
                log.debug("findLibrary(String): URL " + libUrl //$NON-NLS-1$
                        + " resolved as local file " + libFile); //$NON-NLS-1$
            }
            if (libFile.isFile()) {
                result = libFile.getAbsolutePath();
                break;
            }
            continue;
        }
        // we have some kind of non-local URL
        // try to copy it to local temporary file
        libFile = (File) libraryCache.get(libUrl.toExternalForm());
        if (libFile != null) {
            if (libFile.isFile()) {
                result = libFile.getAbsolutePath();
                break;
            }
            libraryCache.remove(libUrl.toExternalForm());
        }
        if (libraryCache.containsKey(libUrl.toExternalForm())) {
            // already tried to cache this library
            break;
        }
        libFile = cacheLibrary(libUrl, libname);
        if (libFile != null) {
            result = libFile.getAbsolutePath();
            break;
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("findLibrary(String): name=" + name //$NON-NLS-1$
                + ", libname=" + libname //$NON-NLS-1$
                + ", result=" + result //$NON-NLS-1$
                + ", this=" + this); //$NON-NLS-1$
    }
    return result;
}

From source file:org.openmrs.module.ModuleClassLoader.java

/**
 * @see java.lang.ClassLoader#findLibrary(java.lang.String)
 *//*from  w w  w  .j  av a2  s.c o m*/
@Override
protected String findLibrary(final String name) {
    if ((name == null) || "".equals(name.trim())) {
        return null;
    }

    if (log.isTraceEnabled()) {
        log.trace("findLibrary(String): name=" + name + ", this=" + this);
    }
    String libname = System.mapLibraryName(name);
    String result = null;
    //TODO
    //PathResolver pathResolver = ModuleFactory.getPathResolver();
    //      for (Library lib : getModule().getLibraries()) {
    //         if (lib.isCodeLibrary()) {
    //            continue;
    //         }
    //         URL libUrl = null; //pathResolver.resolvePath(lib, lib.getPath() + libname);
    //         if (log.isDebugEnabled()) {
    //            log.debug("findLibrary(String): trying URL " + libUrl);
    //         }
    //         File libFile = OpenmrsUtil.url2file(libUrl);
    //         if (libFile != null) {
    //            if (log.isDebugEnabled()) {
    //               log.debug("findLibrary(String): URL " + libUrl
    //                     + " resolved as local file " + libFile);
    //            }
    //            if (libFile.isFile()) {
    //               result = libFile.getAbsolutePath();
    //               break;
    //            }
    //            continue;
    //         }
    //         // we have some kind of non-local URL
    //         // try to copy it to local temporary file
    //         libFile = (File) libraryCache.get(libUrl);
    //         if (libFile != null) {
    //            if (libFile.isFile()) {
    //               result = libFile.getAbsolutePath();
    //               break;
    //            }
    //            libraryCache.remove(libUrl);
    //         }
    //         if (libraryCache.containsKey(libUrl)) {
    //            // already tried to cache this library
    //            break;
    //         }
    //         libFile = cacheLibrary(libUrl, libname);
    //         if (libFile != null) {
    //            result = libFile.getAbsolutePath();
    //            break;
    //         }
    //      }
    if (log.isTraceEnabled()) {
        log.trace("findLibrary(String): name=" + name + ", libname=" + libname + ", result=" + result
                + ", this=" + this);
    }

    return result;
}

From source file:org.opennms.install.Installer.java

/**
 * <p>findLibrary</p>//from   w  ww  . ja  v a 2s  .c  om
 *
 * @param libname a {@link java.lang.String} object.
 * @param path a {@link java.lang.String} object.
 * @param isRequired a boolean.
 * @return a {@link java.lang.String} object.
 * @throws java.lang.Exception if any.
 */
public String findLibrary(String libname, String path, boolean isRequired) throws Exception {
    String fullname = System.mapLibraryName(libname);

    ArrayList<String> searchPaths = new ArrayList<String>();

    if (path != null) {
        for (String entry : path.split(File.pathSeparator)) {
            searchPaths.add(entry);
        }
    }

    try {
        File confFile = new File(
                m_opennms_home + File.separator + "etc" + File.separator + LIBRARY_PROPERTY_FILE);
        final Properties p = new Properties();
        final InputStream is = new FileInputStream(confFile);
        p.load(is);
        is.close();
        for (final Object k : p.keySet()) {
            String key = (String) k;
            if (key.startsWith("opennms.library")) {
                String value = p.getProperty(key);
                value = value.replaceAll(File.separator + "[^" + File.separator + "]*$", "");
                searchPaths.add(value);
            }
        }
    } catch (Throwable e) {
        // ok if we can't read these, we'll try to find them
    }

    if (System.getProperty("java.library.path") != null) {
        for (final String entry : System.getProperty("java.library.path").split(File.pathSeparator)) {
            searchPaths.add(entry);
        }
    }

    if (!System.getProperty("os.name").contains("Windows")) {
        String[] defaults = { "/usr/lib/jni", "/usr/lib", "/usr/local/lib", "/opt/NMSjicmp/lib/32",
                "/opt/NMSjicmp/lib/64", "/opt/NMSjicmp6/lib/32", "/opt/NMSjicmp6/lib/64" };
        for (final String entry : defaults) {
            searchPaths.add(entry);
        }
    }

    System.out.println("- searching for " + fullname + ":");
    for (String dirname : searchPaths) {
        File entry = new File(dirname);

        if (entry.isFile()) {
            // if they specified a file, try the parent directory instead
            dirname = entry.getParent();
        }

        String fullpath = dirname + File.separator + fullname;
        if (loadLibrary(fullpath)) {
            return fullpath;
        }

        if (fullname.endsWith(".dylib")) {
            final String fullPathOldExtension = fullpath.replace(".dylib", ".jnilib");
            if (loadLibrary(fullPathOldExtension)) {
                return fullPathOldExtension;
            }
        }
    }

    if (isRequired) {
        StringBuffer buf = new StringBuffer();
        for (final String pathEntry : System.getProperty("java.library.path").split(File.pathSeparator)) {
            buf.append(" ");
            buf.append(pathEntry);
        }

        throw new Exception("Failed to load the required " + libname
                + " library that is required at runtime.  By default, we search the Java library path:"
                + buf.toString() + ".  For more information, see http://www.opennms.org/index.php/" + libname);
    } else {
        System.out.println("- Failed to load the optional " + libname + " library.");
        System.out.println(
                "  - This error is not fatal, since " + libname + " is only required for optional features.");
        System.out.println("  - For more information, see http://www.opennms.org/index.php/" + libname);
    }

    return null;
}