This discussion is archived
9 Replies Latest reply: Dec 26, 2011 2:25 PM by jschellSomeoneStoleMyAlias RSS

UnsatisfiedLinkError: Can't find dependent libraries

Sreram Newbie
Currently Being Moderated
Hello,

Am currently developing an application based on JNI. I have, after linking required libraries in c and compiling it, created a dll file 'hidapi.dll', which i load in java program via System.loadLibrary("hidapi") and compile it. When i run it in my machine (after of course, putting the dll file in System32 folder - Am using Wn 7 for your info.. ), the code runs perfectly fine. So far so good.

But when i put the dll file in another machine the dll file is not getting loaded... My code gives an exception java.lang.UnsatisfiedLinkError: C:\Windows\System32\hidapi.dll: Can't find dependent libraries... When i tried to have the verbose messages through -verbose:jni , I get the following message.

[Dynamic-linking native method java.io.FileOutputStream.writeBytes ... JNI]
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: C:\Window
s\System32\hidapi.dll: Can't find dependent libraries
[Dynamic-linking native method java.lang.Throwable.getStackTraceDepth ... JNI]
[Dynamic-linking native method java.lang.Throwable.getStackTraceElement ... JNI]
at java.lang.ClassLoader$NativeLibrary.load(Native Method)[Dynamic-linking native method sun.awt.windows.WComponentPeer.nativeHandleEvent ... JNI]
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 hidsignalapi.UsbPctoolJni.<clinit>(UsbPctoolJni.java:46)

hidsignalapi.UsbPctoolJni is the class where i load the dll file, and line 46 is exactly the line where i wrote 'System.loadLibrary("hidapi");'

I saw in a few forums to ensure the class names are correct... But if that is the case, it must not run in my system too right? The only linked files are jni.h and a custom library hidapi.h. Should the hidapi.h too must be put in classpath? Has any body has any idea about this..?

Thanks,
Sreram
  • 1. Re: UnsatisfiedLinkError: Can't find dependent libraries
    gimbal2 Guru
    Currently Being Moderated
    Apparently your DLL depends on another DLL that does not exist on the second machine. Might be a missing Visual C++ runtime for example.
  • 2. Re: UnsatisfiedLinkError: Can't find dependent libraries
    Sreram Newbie
    Currently Being Moderated
    Ya thats the exact conclusion i have come too, but I don't see any linking to C++ libraries. Am using only win32 C api for calling usb related functions (The project is about transferring files to USB). It uses setupapi.dll file to load and perform usb related functions like enumeration. Should we manually load 'setupapi.dll' file too?

    Thanks,
    Sreram
  • 3. Re: UnsatisfiedLinkError: Can't find dependent libraries
    EJP Guru
    Currently Being Moderated
    I don't see any linking to C++ libraries
    You get that by default in Visual C unless you tell it otherwise.
  • 4. Re: UnsatisfiedLinkError: Can't find dependent libraries
    Sreram Newbie
    Currently Being Moderated
    <quote>You get that by default in Visual C unless you tell it otherwise.</quote>

    Oops! So how do i tell it otherwise? Is there any configuration setting in VS 2010 to specify not to use Visual C++? I were seeing in many forums to change the target machine and the sort, but nothing works for me. And I couldn't find the specific single library, which actually causes this problem. If I find that may be i can load that dll in my application before the dll file i must be using, so as to prevent the error.

    I previously thought some Microsoft SDKs might be included in the dll file, so tried installing Microsoft SDK - which in turn requires .Net 3.5 SP1, which my manager is basically against as this is a Java project - in a system and tried running the app, again in vain.

    Thanks,
    Sreram
  • 5. Re: UnsatisfiedLinkError: Can't find dependent libraries
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    user13333704 wrote:
    Is there any configuration setting in VS 2010 to specify not to use Visual C++?
    It is technically possible to create a C/C++ application without using compiler libraries but it that is an esoteric subject that has nothing to do with anything you might be doing with JNI.

    There are only two options: dynamic linking or static linking. The first means that you deliver or expect libraries on the system besides your binary. The second adds the libraries code into your binary.

    That also presumes that the library that you are linking to is available as part of the C/C++ compiler library.

    I am rather certain that the missing component, hidapi.dll, has nothing to do with C/C++ compiler libraries. And unless you have a static library for that, which is not the same as the dll, there is no way you are going to link to that statically. Even presuming that you are using that directly. Versus its use by another library over which you have no control.

    Realistically what it really comes done to is that you must understand what dependencies your own application has first before you explore the options for delivering it.
  • 6. Re: UnsatisfiedLinkError: Can't find dependent libraries
    Sreram Newbie
    Currently Being Moderated
    It is technically possible to create a C/C++ application without using compiler libraries but it that is an esoteric subject that has nothing to do with anything you might be doing with JNI.
    That is exactly, being a java programmer, where am stuck.
    I am rather certain that the missing component, hidapi.dll, has nothing to do with C/C++ compiler libraries.
    Yes. I, in my Java code, am loading the hidapi.dll dynamically, to perform some communication with the USB device, which is a JNI C dll, I created using VS2010 and the C code uses the static library setupapi.lib (for communicating directly with the OS).
    And unless you have a static library for that, which is not the same as the dll, there is no way you are going to link to that statically.
    I have MS SDK V7.0A pre-installed (along with Visual Studio - VS2010) in my system which has the library. So it works in my System (Windows 7). Problem arises only when i try to run the same code in another system, which has no VS.
    Realistically what it really comes done to is that you must understand what dependencies your own application has first before you explore the options for delivering it.
    That is exactly what i am trying to do here.

    Now i have a few questions here.

    One: Do i need to install SDK in every system for the static linking and in tun my code to work? Or will it suffice if i put the setupapi.lib in path? Or should i convert the static linking to dynamic linking? (I realize i am asking more of C related questions in a Java forum, but I am sure your answers will help me in creating a working code.)

    Two: After pathetically failing to create a working dll with VS2010, I tried to compile the code using Mingw gcc compiler and my code (C & JNI code) compiles successfully, but I do not know how to create a dll file by properly linking with the library files.

    After compiling the code with the -c option, creating hid.o for the hid class and hidapi_wrap.o for the JNI wrapper, I tried to create a dll with the following command.

    D:\Sreram>gcc -Wl,-kill-at -o hidapi.dll hidapi_wrap.o hid.o
    hid.o: In function `hid_enumerate':
    D:\Sreram/hid.c:286: undefined reference to `_imp__SetupDiGetClassDevsA@16'
    D:\Sreram/hid.c:295: undefined reference to `_imp__SetupDiEnumDeviceInterfaces@20'
    D:\Sreram/hid.c:310: undefined reference to `_imp__SetupDiGetDeviceInterfaceDetailA@24'
    D:\Sreram/hid.c:324: undefined reference to `_imp__SetupDiGetDeviceInterfaceDetailA@24'
    D:\Sreram/hid.c:463: undefined reference to `_imp__SetupDiDestroyDeviceInfoList@4'
    c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../libmingw32.a(main.o): In function `main':
    C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to `WinMain@16'
    collect2: ld returned 1 exit status

    Obvious that the linker has no clue about these functions which are defined in external library 'Setupapi.lib' & setupapi.dll and hence I tried linking the file by adding the command '-lsetupapi.lib' (without 's), '-llibsetupapi.a' and -l'setupapi.dll' in vain. The compiler says

    c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -llibsetupapi.a (with whatever linker option i give)
    collect2: ld returned 1 exit status

    What am i doing wrong here?

    And three: Even if i succeed in creating a working dll, will it work in other Windows operating systems - I know i had to create a separate library file (.so) in linux and whatever in mac - like Windows XP, Windows Server 2003, etc.?

    Thanks for your patient replies and excuse me for such a long reply.

    Sreram
  • 7. Re: UnsatisfiedLinkError: Can't find dependent libraries
    EJP Guru
    Currently Being Moderated
    Obvious that the linker has no clue about these functions which are defined in external library 'Setupapi.lib' & setupapi.dll and hence I tried linking the file by adding the command '-lsetupapi.lib' (without 's), '-llibsetupapi.a' and -l'setupapi.dll' in vain. The compiler says

    c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -llibsetupapi.a (with whatever linker option i give)
    collect2: ld returned 1 exit status

    What am i doing wrong here?
    I think you could have worked that out for yourself. The compiler is treating the entire -llibsetupapi.a as a library name to find. Get rid of the '-l' and 'lib' parts of that.
  • 8. Re: UnsatisfiedLinkError: Can't find dependent libraries
    Sreram Newbie
    Currently Being Moderated
    I think you could have worked that out for yourself. The compiler is treating the entire -llibsetupapi.a as a library name to find. Get rid of the '-l' and 'lib' parts of that.
    I couldn't do that as i were building it with Eclipse C/C++ IDE and there were no options in linker for it (At least i couldn't find the right place for it).

    Anyways i solved the problem. I will try to brief the problems faced by me and the way i solved it, as it might be useful to other people also.

    First as I couldn't give the lib file name without the -l option in eclipse, i tried building the exe directly in command prompt, for the purpose of checking whether the libraries get linked properly, but the exe was giving 'Not a valid Win32 application' error, and the built dll too weren't good as i were again getting UnsatisfiedLinkError, bu this time with 'hid_enumerate(II)J' which indicated that although the dll file gets loaded properly, it still doesn't link properly for its execution.

    So i again started searching many forums again for UnsatisfiedLinkError, which told me how to write jni code and build it, which i had already done but still not running.

    So i tried searching for 'Not a valid Win32 application' where in one forum i saw that there might be a conflict with C++ libraries (again C++ libraries!), and realized I installed C++ compiler too in Mingw. So I completely uninstalled Mingw (I first tried uninstalling only C++ in vain which is another story), deleted Mingw folder, then installed only C compiler, compiled and created the dll file (again not in eclipse but in command prompt), loaded setupapi.dll file with System.LoadLibrary("setupapi") before loading the created dll file - just in case, and then ran my jar file.

    Finally the application began to run. But still the exe file created from the compilation of C code gives 'Not a valid Win32 application' error. But as am worried only about my app, didn't bother about exe file not running.

    Thank you all for all your support and suggestions. Hope this solution helps other people banging their head because of the same problem.

    Edited by: user13333704 on Dec 19, 2011 8:37 PM
  • 9. Re: UnsatisfiedLinkError: Can't find dependent libraries
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    user13333704 wrote:
    Two: After pathetically failing to create a working dll with VS2010,
    I use VS because it is so easy to do it.
    1. Run VS
    2. File->New->Project
    3. "Visual C++"->Win32. (You must NOT use CRL, but ATL might work.)
    4. ->"Win32 Project"
    5. Fill in name and location
    6. Hit Ok
    7. Hit "Next>"
    8. Select "Dll"
    9. Hit "Finish"

    It spins until project is done.

    Then right click the project name and select properties. Under "Configuration Properties"->"VC++ Directories"->"Library Directories" to add the JNI lib dir.
    Same for "Include Directories" for jni.h

    Then under "Linker"->"Input"->"Additional Dependencies" add the JNI dll

    Repeat above steps as needed to add other libraries.

    That is probably all that is needed.

    Note that without some understanding of what the above means and the ability to investigate other possible characteristics of C/C++ programming (not Java) then attempting even something trivial in JNI is going to be at best difficult if not impossible.
    I tried to compile the code using Mingw gcc compiler
    One advantage to that of course is that you are going to have to actually figure out what the command line options mean. The above VS example does that for you - there is even a place in the properties that shows you what the complete command line looks like.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points