Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access dll functions in java

 
K.Suresh Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K.Suresh Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Believe it or not, it's not actually Peter's job to write your code... have you tried this?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21364
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21364
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is becoming a FAQ, I have collected some information and relevant links here.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21364
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic