iterate-ch / rococoa

Rococoa allows you to call Objective-C code through Java classes and interfaces that you define.
GNU Lesser General Public License v3.0
74 stars 18 forks source link

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

Closed GoogleCodeExporter closed 4 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

doompadee commented 5 years ago

Works for me with JNA 5.2. I just had to get the directory structure right. JNA seems to be looking under /darwin/librococoa.dylib.