Java and DLL's
How can I call a DLL and invoke it's methods from a java class? The DLL was written in Visual Fox Pro and it's class definition is OLEPUBLIC so it should be accessible to any automation client.
How can I call a DLL and invoke it's methods from a java class? The DLL was written in Visual Fox Pro and it's class definition is OLEPUBLIC so it should be accessible to any automation client.
Example (things dont have to be private):
public class NativeCaller{
static {
// note that you dont put the ".dll" ext
System.loadLibrary("MyDLL");
}
private NativeCaller(){}
// call to native method
private static native void MethodName();
}
use:
javah -jni NativeCaller
to create header file
When running you might need this command line flag:
-Djava.library.path=.
For your java code to call a DLL you will have to write native code
to call its methods. Basically you have to write native code
between the DLL and your program. The javah command creates
the .h header file for you to use.
I am not sure I understand the native code part. Can you explain in more detail, give me an example, or point me to an example or tutorial?
> I am not sure I understand the native code part. Can
> you explain in more detail, give me an example, or
> point me to an example or tutorial?
To start with you need the following.
A C or C++ compiler.
A basic understanding of the C/C++ language.
Knowledge on how to create shared libraries (dll) with the compiler.
You can't do JNI if you don't know the above.
Learning the above has nothing to do with java.
If you search in the JNI forum then there are libraries (java) that help you use dlls without using JNI yourself. Those might or might not help you.
It's important to understand you can't directly link java to any old DLL, it has to have the right call structure, and calls are generally required to specialised code to translate between Java and C data structure.
You can, however, write a JNI DLL which loads and calls any standard DLL.
You start by writing the Java class in which the native method signatures are defined. You run that through javah to get a C header file to define what the coresponding signatures in C.