2

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 Project > Java Build Path > Add External JARs to import that jar.

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 that code runs, 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 can be imported.

This is the code snippet I'm trying to run:

    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.

Jonathan Simon Prates
  • 1,122
  • 2
  • 12
  • 28
  • 1
    See [How to add native library to “java.library.path” with Eclipse launch](http://stackoverflow.com/q/661320/1048330) – tenorsax Aug 22 '12 at 17:48

1 Answers1

4

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();
    }
  }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Many thanks in advance. My JRE is x64. I install 32bits version and works! Thank you! – Jonathan Simon Prates Aug 23 '12 at 11:37
  • 1
    You saved me! JVM is 32 but Windows is 64 so I had to put jmtp.dll on C:\Windows\SysWOW64 to work. – Bagata Sep 01 '14 at 20:11
  • @Aqua, I'm still having a problem, maybe you can help. If I put the DLL manually it works but if I try using java to write the DLL before loading the Library, the exception "Can't find dependent libraries" appears again. – Bagata Sep 02 '14 at 17:57
  • @Bagata what do you mean by *java to write the DLL before loading the Library* ? – tenorsax Sep 02 '14 at 18:53
  • I have an applet that copies the DLL from the server and writes it into temp directory right before using System.load("jmtp"). I use a hack to change java.library.path to temp directory. – Bagata Sep 02 '14 at 18:57
  • @Bagata are you sure the file is copied, applets are pretty restricted. – tenorsax Sep 02 '14 at 19:06
  • @Aqua Yeah, I'm sure... that's why I don't know why it is not working. Both the content and size of the copied DLL match the original. – Bagata Sep 02 '14 at 19:14
  • @Bagata I am not sure you can modify `java.library.path` at runtime. Is that temp directory added to a library path dynamically or is it set at application execution? – tenorsax Sep 02 '14 at 19:21
  • @Aqua Using the hack I set java.library.path to temp at runtime, it works because it returns temp directory when I print System.getProperty("java.library.path"); – Bagata Sep 02 '14 at 19:29
  • 1
    @Bagata Even though the property is updated the loader inspects that property only once at startup. See [this](http://stackoverflow.com/q/5419039/1048330) thread for more details. – tenorsax Sep 02 '14 at 19:37