Java Code Examples for com.sun.jna.Native#load()

The following examples show how to use com.sun.jna.Native#load() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CudaUtils.java    From djl with Apache License 2.0 5 votes vote down vote up
private static CudaLibrary loadLibrary() {
    try {
        if (System.getProperty("os.name").startsWith("Win")) {
            String path = System.getenv("PATH");
            if (path == null) {
                return null;
            }
            Pattern p = Pattern.compile("cudart64_\\d+\\.dll");
            String[] searchPath = path.split(";");
            for (String item : searchPath) {
                File dir = new File(item);
                File[] files = dir.listFiles(n -> p.matcher(n.getName()).matches());
                if (files != null && files.length > 0) {
                    String fileName = files[0].getName();
                    String cudaRt = fileName.substring(0, fileName.length() - 4);
                    logger.debug("Found cudart: {}", files[0].getAbsolutePath());
                    return Native.load(cudaRt, CudaLibrary.class);
                }
            }
            logger.debug("No cudart library found in path.");
            return null;
        }
        return Native.load("cudart", CudaLibrary.class);
    } catch (UnsatisfiedLinkError e) {
        logger.debug("cudart library not found.");
        logger.trace("", e);
        return null;
    }
}
 
Example 2
Source File: GnomeKeyringLibrary.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
private static GnomeKeyringLibrary load(Map<String,?> options) {
    try {
        return Native.load(GENERIC, GnomeKeyringLibrary.class, options);
    } catch (UnsatisfiedLinkError x) {
        // #203735: on Oneiric, may have trouble finding right lib.
        // Precise is using multiarch (#211401) which should work automatically using JNA 3.4+ (#211403).
        // Unclear if this workaround is still needed for Oneiric with 3.4, but seems harmless to leave it in for now.
        if (new File(EXPLICIT_ONEIRIC).isFile()) {
            return Native.load(EXPLICIT_ONEIRIC, GnomeKeyringLibrary.class, options);
        } else {
            throw x;
        }
    }
}
 
Example 3
Source File: JnaBuilderTest.java    From alpha-serac with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    // get lib folder from resource
    URL url = JnaBuilderTest.class.getClassLoader().getResource("lib");
    if (Objects.isNull(url)) {
        return;
    }
    File file = new File(url.getFile());

    // set jna.library.path to the path of lib
    System.setProperty("jna.library.path", file.getAbsolutePath());
    // load example.so from lib
    ExampleJNA example = Native.load("example", ExampleJNA.class);

    // call sum method from example library
    int a = 2;
    int b = 3;
    int result = example.sum(a, b);
    log.info(a + " + " + b + " = " + result);

    // example using arrays
    int size = 5;
    // input 2 integer arrays
    int[] listA = new int[]{1, 2, 3, 4, 5};
    int[] listB = new int[]{5, 4, 3, 2, 1};
    // output the sum of the arrays
    int[] listC = new int[size];
    example.sumArray(listA, listB, listC, size);
    log.info("A = " + Arrays.toString(listA));
    log.info("B = " + Arrays.toString(listB));
    log.info("A + B = " + Arrays.toString(listC));

}
 
Example 4
Source File: MacUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Load Core Foundation framework.
 *
 * @return JNA interface of the Core Foundation framework
 */
private static CoreFoundation loadCoreFoundation() {
    try {
        //LOGGER.debug("Load Core Foundation framework.");
        return Native.load(CoreFoundation.JNA_LIBRARY_NAME, CoreFoundation.class);
    } catch (UnsatisfiedLinkError ex) {
        LOGGER.warn("Can't load Core Foundation framework.", ex);
        return null;
    }
}
 
Example 5
Source File: MacUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Load Core Graphics framework.
 *
 * @return JNA interface of the Core Graphics framework
 */
private static CoreGraphics loadCoreGraphics() {
    try {
        //LOGGER.debug("Load Core Graphics framework.");
        return Native.load(CoreGraphics.JNA_LIBRARY_NAME, CoreGraphics.class);
    } catch (UnsatisfiedLinkError ex) {
        LOGGER.warn("Can't load Core Graphics framework.", ex);
        return null;
    }
}
 
Example 6
Source File: WindowsUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Load user32 library.
 *
 * @return JNA interface of the user32 library
 */
private static User32 loadUser32() {
    try {
        //LOGGER.debug("Load user32 library.");
        return Native.load(User32.class);
        //return Native.load(User32.class, W32APIOptions.DEFAULT_OPTIONS);
        //return Native.load(User32.class, W32APIOptions.UNICODE_OPTIONS);
    } catch (UnsatisfiedLinkError ex) {
        LOGGER.warn("Can't load user32 library.", ex);
        return null;
    }
}
 
Example 7
Source File: LinuxUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Load X11 library.
 *
 * @return JNA interface of the X11 library
 */
private static X11 loadX11() {
    try {
        //LOGGER.debug("Load X11 library.");
        return Native.load(X11.class);
    } catch (UnsatisfiedLinkError ex) {
        LOGGER.warn("Can't load X11 library.", ex);
        return null;
    }
}
 
Example 8
Source File: LinuxUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Load XTest library.
 *
 * @return JNA interface of the XTest library
 */
private static X11.XTest loadXTest() {
    try {
        //LOGGER.debug("Load XTest library.");
        return Native.load(X11.XTest.class);
    } catch (UnsatisfiedLinkError ex) {
        LOGGER.warn("Can't load XTest library.", ex);
        return null;
    }
}
 
Example 9
Source File: LibUtils.java    From djl with Apache License 2.0 4 votes vote down vote up
public static MxnetLibrary loadLibrary() {
    String libName = getLibName();
    logger.debug("Loading mxnet library from: {}", libName);

    return Native.load(libName, MxnetLibrary.class);
}
 
Example 10
Source File: OSXNotifier.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public OSXNotifier() {
    cf = Native.load("CoreFoundation",CoreFoundation.class);    //NOI18N
    cs = Native.load("CoreServices",CoreServices.class);        //NOI18N
    callback = new EventCallbackImpl();
    events = new LinkedBlockingQueue<String>();
}
 
Example 11
Source File: LinuxNotifier.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public LinuxNotifier() {
    IMPL = Native.load("c", InotifyImpl.class);
}