How to configure .dll file in Java?

2020-02-10 06:49发布

问题:

I am using Jacob jar file in my java application.

This Jacob jar file comes with a .dll file. I have added Jacob jar file to my classpath. But when I execute my application a runtime error occurs as

"couldn't load jacob-1.15-M3-x86.dll file"

How can I load this .dll file?

Edited:=================================================================================

I had set the "path" environment varaible to the dir that contains my .dll file and loading that .dll file as follows

static {
    System.loadLibrary("jacob-1.15-M3-x86.dll");
}

but the following error occured

    java.lang.UnsatisfiedLinkError: no jacob-1.15-M3-x86.dll in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1028)
    at TemplateClass.TemplateClass.<clinit>(TemplateClass.java:14)

回答1:

The 'jacob-1.15-M3-x86.dll' needs to be in a place where your the operating system can find it. You have a few options here:

  • You can place the .dll file in the directory you started your application from. If you have a batch script to start your application, it would be that directory. If you are starting in some sort of application server, it would typically be the 'bin' directory.

  • You can place the .dll file somewhere in the %PATH% environment variable. I may be easier to just update your PATH environment variable to include the directory that contains your .dll file.

  • Another option is to place your .dll into the %SystemRoot%\system32 directory. Usually this is 'C:\Windows\system32'. This option is not usually recommended unless it is a shared library like the MSCVRT runtime.

One other possible issue you might have. If the .dll is compiled as 32-bit, then you must be running in the 32-bit Java runtime. Likewise, if it is a 64-bit .dll it needs to be run in a 64-bit JRE.



回答2:

Ah, that's not a compilation error but a runtime error.

My guess would be that your DLL needs to be on the PATH. Not CLASSPATH, but PATH, because that's where Windows looks for DLLs. Try either extending your PATH to include the location of your DLL, or do what many other people do: Dump the DLL into \Winnt\System\System32 or whatever the system directory is called on your box. Wherever all the other DLLs are, in other words.

Update

The error message you post, thankfully, is pointing out the exact problem. You can solve it by putting the directory containing your DLL into java.library.path This Sun forum thread shows a nice example: http://forums.sun.com/thread.jspa?threadID=627890

Actually, that's a lot less clean than it should be; this seems to be one of the "shadier" areas in Java. The thread wanders around a lot, I do advise you to read all the way through to see some problems and solutions. I think you'll be able to succeed with a little trial and error.



回答3:

Other options :

  • set the property java.library.path to the directory containing the dll. Example : java -Djava.library.path="path/to/directory/containing/the/dll" -jar appli.jar
  • in the code, load the dll explicitly, with System.load.


回答4:

You need to set LD_LIBRARY_PATH. This will give you all the right steps to follow.



回答5:

When you use System.loadLibrary() don't include the .dll at the end.

Also, if you are not setting java.library.path to point to the folder containing the DLL then the DLL should be in the directory where you launch your Java application from.



回答6:

I had the same problem.

I see that the question is not "answered", so maybe none of the options above worked.

One of my last hypothesis was that the Jacob.dll is missing its dependency.

What I did was to get depend and check if all the dependence, used by Jacob are loaded. Of course this works for Windows.

Cheers!