Search code examples
javac++dlljava-native-interfaceusb

Java: util_USBUIRT.dll: Can't find dependent libraries


I want to use this jar file (http://sourceforge.net/projects/uirt-j/) in a personal project. Currently, I've been using Eclipse and tried to import that jar (Project > Java Build Path > Add External JARs).

After importing it, I can see all classes from that package listed in Eclipse, however, this jar also contains two win32 dll files, needed to communicate to the device. I've tried to add them to System32 dir, but no luck. When I run this project, it throws the following exception:

    Exception in thread "main" java.lang.UnsatisfiedLinkError:
    C:\Windows\System32\util_USBUIRT.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at util.USBUIRT.<clinit>(USBUIRT.java:269)
    at Uirt.main(Uirt.java:6)

Using dependence walker, I can see that all the dlls are correctly linked and they can be imported.

Code snippet:

    import util.USBUIRT;
    public class Uirt {
    public static void main(String[] args) {
        String code = "0000";   
        try {
            USBUIRT.transmitIR(code, 2, 3, 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
      }
    }

If that JAR file is executed standalone, it works fine. My current setup runs under Windows 7 64bits.


Solution

  • The dlls in the mentioned jar are 32 bit. The environment is Win7 x64. I assume the JVM is 32 bit otherwise there would be another error, ie: Can't load IA 32-bit .dll on a AMD 64-bit platform or similar.

    Try copying the dlls into C:\Windows\SysWOW64 rather than C:\Windows\System32. 32 bits dlls should go into C:\Windows\SysWOW64. It worked for me, although I got util.USBUIRT$NotInitializedException which is probably the indication the libraries were loaded properly.

    File System Redirector article may shed some light on SysWOW64 vs System32.

    EDIT: tweaking java.library.path

    You may also go with a solution mentioned in comments, for example, copy dlls into C:\tmp and run with argument:

    -Djava.library.path="C:\tmp;${env_var:PATH}"
    

    But since there is a dependency between the two dlls, C:\tmp must be on PATH. Otherwise there is still UnsatisfiedLinkError. Manually loading uuirtdrv.dll should help, ie:

    import util.USBUIRT;
    public class Uirt {
        static {
            System.loadLibrary("uuirtdrv");
        }
    
    public static void main(String[] args) {
        String code = "0000";   
        try {
            USBUIRT.transmitIR(code, 2, 3, 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
      }
    }