Login  Register

Re: Loading native DLLs into InageJ

Posted by Burger Wilhelm on Sep 23, 2015; 4:19pm
URL: http://imagej.273.s1.nabble.com/Loading-native-DLLs-into-InageJ-tp5014423p5014425.html

Hello John,

thanks for the quick response. No, I  am not loading the DLL directly from my plugin but through a static block in another class. Here is my (minimalistic) setup:

First, there is this 'DllLoader' class which I placed in a separate JAR file. It should not be reloaded when a plugin is loaded:

// ----------------------------------------------------------
public class DllLoader {
        static {
                String dllname = "opencv_java300";
                System.out.println("DllLoader: loading native library");
                try {System.loadLibrary(dllname);}
                catch (UnsatisfiedLinkError ule) {
            System.out.println("Failed to load " + dllname);
            ule.printStackTrace(System.out);}
        }

        public static void main(String[] args) {}
}
// ----------------------------------------------------------

And this is my stripped-off ImageJ test plugin:

// ----------------------------------------------------------
import ij.IJ;
import ij.plugin.PlugIn;

public class Dll_Load_Test implements PlugIn {
       
        public void run(String args) {
                loadDll();
                // use native methods ...
        }
       
        void loadDll() {
                try {
                        Class.forName("DllLoader");
                } catch (ClassNotFoundException e) {
                        System.out.println("DllLoader not found!");
            e.printStackTrace(System.out);
                }
        }
}
// ----------------------------------------------------------

Does this make any sense to you?

--Wilhelm


> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> John Hayes
> Sent: Wednesday, September 23, 2015 4:30 PM
> To: [hidden email]
> Subject: Re: Loading native DLLs into InageJ
>
> Hi Wilhelm,
>
> It's been a while since I looked at the codebase, but I believe the IJ startup
> classloader is going to be different than the one used during "compile and
> run" and that would be consistent with your error message. You can check
> for references to each by putting in your plugin code by logging/printing
> something like "Your_Plugin.getClass().getClassLoader()" under the working
> and non-working conditions to discriminate them.
>
> Are you explicitly loading the DLL from your plugin? If so, what is that code?
>
> Best,
>
> John
>
> Le 23 sept. 2015 à 10:19, Burger Wilhelm a écrit :
>
> > Hello all, I wonder if someone can help me with the following technical
> problem:
> >
> > I am loading a native DLL into ImageJ (e.g., for using OpenGL or OpenCV,
> under Windows). Everything works nicely if I execute precompiled plugins
> that use the native methods in that DLL, even repeatedly. However, when I
> execute the same plugin with "Compile and run" I get the following errors:
> >
> > java.lang.UnsatisfiedLinkError: Native Library XXX.dll already loaded in
> another classloader
> >
> > As a remedy I tried to load the DLL by a static block of a special class, placed
> in a separate JAR file (read somewhere that this might help), and referenced
> that class "by name" - but with no avail.
> >
> > I am not sure I understand well enough how class loaders and DLLs work
> together but assume somebody else must have encountered the same mess
> before. Any hints or suggestions for how to solve this?
> >
> > Thanks much,
> > Wilhelm
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html