nagyistoce / rococoa

Automatically exported from code.google.com/p/rococoa
0 stars 0 forks source link

Load librococoa.dylib from JAR file - no java.library.path change required #15

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
In theory, you should be able to load the dylib file from the rococoa jar file. 
This means that library users will not have to change java.library.path. The 
code that should allow this to happen is shown below. Note that it uses 
commons-io but another I/O mechanism could be used.

    private static void loadLibrary() {
        try {
            // have to use a stream
            InputStream in = RococoaManager.class.getResourceAsStream("/lib/librococoa.dylib");
            // always write to different location
            File fileOut = File.createTempFile("lib", ".dylib");
            OutputStream out = FileUtils.openOutputStream(fileOut);
            IOUtils.copy(in, out);
            in.close();
            out.close();
            System.load(fileOut.toString());
        } catch (Exception e) {
            throw new RuntimeException("Failed to load rococoa native library", e);
        }
    }

Original issue reported on code.google.com by antonmar...@gmail.com on 17 Mar 2013 at 11:06

GoogleCodeExporter commented 9 years ago
I think Native.loadLibrary() may already provide this functionality, but you 
have hard-coded the name of the library and it is currently not capable of 
loading a JAR resource.

Original comment by antonmar...@gmail.com on 17 Mar 2013 at 11:14