Link to home
Start Free TrialLog in
Avatar of rnicholus
rnicholus

asked on

"UnsatisfiedLinkError" when calling .NET dll from a Java program.

Hello,

I'm trying to call a .NET dll from a Java program using JNI. I attach the code snippet.
I keep getting "UnsatisfiedLinkError" when I call the GeoCoder() method from the library.
I'm not sure what I'm doing wrong. I'm pretty new at JNI so I'm afraid I'm just missing something trivial here.

Thanks in advance for the help.
public class TestGeocoder
{
static
{
System.load("C:\\tcprojects\\JNI\\library1.dll");
System.load("C:\\tcprojects\\JNI\\library2.dll");	
}
	
public static void main(String[] args)
{
TestGeocoder t = new TestGeocoder();
t.GeoCoder();
} // end main(String[])
	
private native void GeoCoder();
 
} // end class

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

try

System.load("/tcprojects/JNI/library1.dll");

ASKER CERTIFIED SOLUTION
Avatar of mbodewes
mbodewes
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi
UnsatisfiedLinkError occurs when the native code is not available or is not found.
Check if the location of the dll is in the path.
also attached some steps for jni
jni-methods.txt
Avatar of rnicholus
rnicholus

ASKER

formula1act,

I'm pretty sure the DLL is in the path. The error is in the method calling.

-----------------------------------------------;

mbodewes,

Could you please explain more about this? i'm still confused. maybe because i'm still very new in this area. These DLLs that I will use is from a vendor that only have .NET solution. Meanwhile, we use everything JAVA/ open source solutions.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
You cannot just call any method within a native .dll. The .dll has to be created using the JNI tools (using JNI arguments). That .dll can then load and use the existing .dlls. Note that you will have to include the manifest within your .NET dll if you create the DLL using Visual Studio as well.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

If they are not JNI DLL's then you'll need to create JNI DLL's to act as a bridge between java and the .NET DLL's.

formula1act: although Java can call .dll's it has to manage between Java and C/C++ data structures. This is done within JNI. Basically with JNI you create a native method in Java which you implement in C/C++ as part of a .dll, which in turn can be loaded by the JVM. The code in the JNI .dll now can use native C/C++ libraries, either by static linking or by dynamic linking (loading other .dll's). That last thing is what you want to do.

Now you have a few options:
- learn to code JNI (the JNI tutorial is pretty good)
- look for an (open source?) third party solution to directly call .dll's
- look for an (open source?) third party solution to directly call .NET code

I haven't got too much experience with the second two options, JNI is working fine for me.

objects: that's rewording of my previous post :)
rnicholus:

In JNI, methods are exposed as DLL's or SO (Shared Objects on Unix flavors)
The native method that you call must be present in the DLL, which is created from the C file using a GCC compiler or any other compiler.
Make sure GeoCoder() method is present in the DLL or create a DLL over your C file that has the method
Sorry, my previous reply was for the original poster of the question of course, rnicholus.
>>>
> If they are not JNI DLL's then you'll need to create JNI DLL's to act as a bridge
> between java and the .NET DLL's.
>>>
Can you give me some initial pointers as how to do this?
You create the .dll using the JNI (see the JNI tutorials), load the .net .dll's and call the methods in there using either c or c++. The Java classes have native methods in them which you can implement using the generated header file(s). These methods in turn call the methods in the other .dll. There isn't much more to it. As said, the JNI tutorials come highly recommended.
Good solution based on my initial research. I haven't got a chance to dig deeper and test.