aspose file tools*
The moose likes Java in General and the fly likes Access dll functions in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Access dll functions in java" Watch "Access dll functions in java" New topic
Author

Access dll functions in java

K.Suresh Kumar
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
Hi Friends,
I want to access the DLL from java, the dll created by dot net application and it have some methods. So I need to call that methods from java.

I try to load dll in java it shows some error message,
The code is
static {
System.load("c:\\Manipulator.dll");
System.out.println("loaded");
}
The error is
java.lang.UnsatisfiedLinkError: C:\Manipulator.dll: A dynamic link library (DLL) initialization routine failed
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.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at com.tps.testcodes.LoadDll.<clinit>(LoadDll.java:9)
Exception in thread "main"

Please help me to solve this issue and i need code for accessing dll in
java.
Thanks in Advance,
Suresh Kumar
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The immediate reason for the exception is that the DLL you are loading has an initialisation function, which is failing. That is specific to that DLL, so you would need to find out what that DLL requires, to be able to initialise successfully.

However, when you get beyond that, do you understand that you can't just call arbitrary DLL functions from Java directly? You will need to create your own interface, using Java Native Interface (JNI). Often, one creates a fairly small JNI library, whose purpose is to translate calls between the forms required by JNI and the form exposed by the DLL.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
K.Suresh Kumar
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
Hi Peter Chase,
Thanks for your immediate reply. But sorry I couldn't understand your answer. Please tell me what I have to do for accessing dll from java program.
could you please explain the steps to create the program or send me the sample code..

Thanks & Regards,
Suresh Kumar.K
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Believe it or not, it's not actually Peter's job to write your code... have you tried this?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by K.Suresh Kumar:
But sorry I couldn't understand your answer.


Then I suspect you have a lot of learning to do before you can achieve your objective. The JNI reference, posted by another responder would be one good place to look.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Another thing: System.loadLibrary expects library NAMES, not library FILES. So instead of calling System.load("c:\\Manipulator.dll"), you should add C:\ to the PATH and call System.load("Manipulator") instead. The JVM and OS combined will match that to any Manipulator.dll file in the PATH.

The reason for this approach is to make it more platform independant. Let's say you have the same library, as Manipulator.dll for Windows and as Manipulator.so for Linux. You won't need to change your code if you switch from one OS to the other; just make sure your environment (PATH) is updated.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Peter Chase:

Then I suspect you have a lot of learning to do before you can achieve your objective. The JNI reference, posted by another responder would be one good place to look.


Unfortunately, the original poster has a lot of learning on the dot net side as well. JNI will get him to C/C++. C/C++ can't access dot net objects -- at least, not more than simple static method references.

Basically, besides the JNI wrapper that needs to be written, you need to write a Managed C++ wrapper too. This Managed C++ wrapper is a dot net object, that doesn't really need to be instantiated, as it has nothing but static methods. It is this code that will instantiate and use the dot net objects.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Now... as another option, if you are willing to pay for it, there are some third party Java to dot net libraries that did all this work already. Just google for "java to dot net bridge".

Henry
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Since this is becoming a FAQ, I have collected some information and relevant links here.


Ping & DNS - my free Android networking tools app
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Rob Prime:
Another thing: System.loadLibrary expects library NAMES, not library FILES. So instead of calling System.load("c:\\Manipulator.dll"), you should add C:\ to the PATH and call System.load("Manipulator") instead. The JVM and OS combined will match that to any Manipulator.dll file in the PATH.


Yes, but the original poster wasn't calling loadLibrary(), he was calling load(). That does take the file name of the library being loaded. The reason for load() is to allow you to load an arbitrary dynamic library into the JVM's process; there aren't all that many times when that would actually be useful, but the facility is there...

The load() call was failing because the library wasn't expecting just to be randomly loaded into a JVM, and its initialisation failed. Otherwise, it would have been OK to load() the DLL like that. When I say "OK", I mean that it would have succeeded, but it wouldn't have magically enabled access to the facilities of the DLL; as this thread has already pointed out, that's a whole load harder.
K.Suresh Kumar
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
Hi Friends,
I came across the old errors. Now I am facing new errors while I try to load dll in java program it shows following error message,
The code is
class callDll
{
public native String dsp(String val);
static {
System.setProperty("java.library.path", "C:\\Library\\");
System.out.println("path"+System.getProperty("java.library.path"));
}
public static void main(String[] args)
{
callDll obj=new callDll();
String val=obj.dsp("hai");
System.out.println("Hello World!"+val);
}
static {
System.out.println("loaded");
System.loadLibrary("Display");
}
}

The Message Error is
The dynamic Link Library mscoree.dll could not be found in the specified path . (This dll related to dot net framework
but I don't want to install dot net framework).

The Console Error is
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Library\Display.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 callDll.<clinit>(callDll.java:20)

Please help me to solve this issue.........

Thanks in Advance,
Suresh Kumar
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

The dynamic Link Library mscoree.dll could not be found in the specified path . (This dll related to dot net framework
but I don't want to install dot net framework).


In Windows, a DLL can be added to the search path, by adding it to the PATH environment variable.

Henry
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
Make sure that the package that you have your class in has the same structure as in the name of the function in the dll
eg: com_siemens_fpag_fpverify_FAUVerification_FAUVerify() is the name of the function then the caller class would be in
com.siemens.fpag.fpverify.FAUVerification
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
Make sure that the package that you have your class in has the same structure as in the name of the function in the dll
eg: com_siemens_fpag_fpverify_FAUVerification_FAUVerify() is the name of the function then the caller class would be in
com.siemens.fpag.fpverify.FAUVerification
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 417
    
    2

Just reading the error message literally i think some people may be barking up the wrong tree ...

C:\Library\Display.dll: Can't find dependent libraries


is the DLL your loading has a dependency on another DLL that it can't find usually, i.e. not that it can't find the DLL in question or that the signature of the contained functions is incorrect.

In Windows you use something like dependency walker to list all the DLLs your DLL pulls in and if you run it in place you get a big red cross by the ones it can't resolve (so unfortunately that may not be the exact problem as sometimes it will warn of DLLS that are only accessed on obscure code paths your app won't hit i.e. its not aware of what your java app will do)

This kind of thing happens a lot with JNI particularly if you accidently pull in a dev version of a DLL and move to a production PC;-)


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access dll functions in java