Reputation: 543
I want to call a .DLL method in Eclipse. Here is my code :
class TestJNI1 {
public native void LireNoVersion();
public void a() {
System.loadLibrary("tttt.dll");
LireNoVersion();
}
}
public GlobalAction() {
this.setBackground(GlobalPreferences.PANEL_BACKGROUND_COLOR);
new TestJNI1().a();
}
The problem is that I have this error on compilation :
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: tttt.dll at java.lang.Runtime.load0(Unknown Source) at java.lang.System.load(Unknown Source)
I already tried to :
System.loadLibrary(...)
and System.load(...)
UPDATE
I tried to print the java.library.path
and get a path. I put the dll in this path and the error message is more confusing now :
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: D:\My\Exact\Path\tttt.dll: Can't find dependent libraries
Here is the code to print the path :
String property = System.getProperty("java.library.path");
StringTokenizer parser = new StringTokenizer(property, ";");
while (parser.hasMoreTokens()) {
System.err.println(parser.nextToken());
}
Upvotes: 3
Views: 4109
Reputation: 543
Using the Library
interface in the sun.jna
package did the trick :
import com.sun.jna.Library;
import com.sun.jna.Native;
public class DllTest {
public interface IDLL extends Library {
IDLL INSTANCE = (simpleDLL) Native.loadLibrary("tttt", IDLL.class);
int LireNoVersion(); // DWORD LireNoVersion();
}
public static void main(String[] args) {
IDLL sdll = IDLL.INSTANCE;
int nover = sdll.LireNoVersion();
System.out.println("version = " + nover + "\n");
}
}
Still don't know why it didn't worked before.
Upvotes: 0
Reputation: 1046
The first problem was it couldn't find the dll because it wasn't in the path.
The second problem is that it can't find the dependencies on the dll that you are using. Your choices would seem to be
Upvotes: 3
Reputation: 325
Give the absolute path of the file
try {
System.load("C:/myapplication/application.dll");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
Upvotes: 0