Introduction:
In this article I am going to demonstrate shortly, with a simple example, how to use dll libraries inside java applications, in order to test the sample program in this article you need to have at least jdk 5, the Jacob package API, also created dll file with complete knowledge of the structure of the library and its functions.
Attached is the sample code zipped with the Jacob jar file, the Com.dll file and the ReadDLL.java file that uses the Jacob.
“Dynamic link library” is Microsoft’s implementation of the shared library concepts in the Microsoft windows operating system. Dlls considered as a native application, not like any applications.
Using the dll libraries within the .Net framework is such an easy operation, just we need to load the library within the environment, initiate an object from it, then use it like any other object in the program.
Such an immediate operation does not exist for the java environment, so that there are some interfaces and frameworks has been built that allow java programmers to interact with dll libraries and use their functionality within java applications, such as the Jacob, “JACOB is a JAva-COm Bridge that allows you to call COM automation components from Java. It uses JNI internally to make native calls into COM and Win32 libraries. In simple words, you can now call any of the .dll file functions from Java and use the result in your Java program (provided you know which function to call)”, JNI is the Java Native Interface.
To start we need to:
• Load the jacob.dll file into the system path, described later,
• Register your dll file as Com, described later,
• Set the jacob jar file, within the class path of the project,
• Then start loading the Lib in the program, and using it.
Here in this article I am using a Com.dll that I have created using the Visual Studio .Net 2005 /VB, by the help of this article.
Creating Com dll using vs.2005
This Library is used simply to sum, subtract, multiply, and divide two integers passed to its methods, the first three return integers, and the last one return double.
Load the Jacob.dll file:
Just paste the Jacob.dll to the system path in your environment; in mine I have put it into the WINDOWS\System32 folder, some environments require a registration for the Jacob.dll using regsvr32 command.
Register the dll file “the Library”
To register the dll file, we use the “regasm /codebase” command from the Microsoft.Net either from the windows command prompt, or from the .Net command prompt itself as follows:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
The “v2.0.50727” may change from environment to another; you better search for the command to know the right path of it. In other words find the correct path of the regam.exe. you should expect to receive confirmation message that the file has been registered :
Microsoft ® .NET Framework Assembly Registration Utility 2.0.50727.42
Copyright © Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully
Otherwise there must be something wrong with the building of the dll file, or some files are missing in the path of your system, [in case of some error, just google it, so that we are repeating].
To know more about the regasm command refer to: Assemply Registeration Tool
After registration, you are supposed to find the library loaded to the registry, you can check it to see the loaded Strong Library name, in our example it is: Com.Calculation
You can check the registry by the command: regedit from the Run.
Inside the java application:
To go with it, we need to have full description about the dll library we are using, in our example here; we have the description of the Com.Calculation Library as follows:
sum(int x,int y)
Div(int x, int y) // check if y = 0, returned as double
multi(int x, int y)
subtract(int x, int y)
Add the Jacob.jar file to your class path; the following is the code segments of needed operations for using the Library:
First load the library:
//Loading the library:
ActiveXComponent comp=new ActiveXComponent("Com.Calculation");
System.out.println("The Library been loaded, and an activeX component been created");
this may throw an exception (ComFailException: Can't get object clsid from progid) if the library is not registered, or not registered properly.
Then call the functions of the library:
int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
System.out.println("Summation= "+ summation);
int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
System.out.println("Subtraction= "+ subtraction);
int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
System.out.println("Multiplication= "+ multiplication);
double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
System.out.println("Division= "+ division);
using the Dispatch class that is from the jacb package, where the first argument is the component that we need to use its functions, the second is the function name as string, and then as many parameters you need to pass to the function, there are other variations. This may through an exception (ComFailException: Can't map name to dispid).
If some of the library functions (methods) return some kind of struct or a class of some type, then its properties could be accessed using the get method of the Dispatch Class:
Dispatch. get(object, “Property Name”);
it returns value with the data type specified.
Again you need to know the specification of the library.
The full ReadDLL.java follows:
package jacobtest;
import com.jacob.activeX.*;
import com.jacob.com.*;
/**
* This class uses the the Jacob tech
* to use and interact with a Com cmponent
* in a java application
*/
public class ReadDLL {
public static void main(String[] args){
//Loading the library:
ActiveXComponent comp=new ActiveXComponent("Com.Calculation");
System.out.println("The Library been loaded, and an activeX component been created");
int arg1=100;
int arg2=50;
//using the functions from the library:
int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
System.out.println("Summation= "+ summation);
int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
System.out.println("Subtraction= "+ subtraction);
int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
System.out.println("Multiplication= "+ multiplication);
double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
System.out.println("Division= "+ division);
/**The following code is abstract of using the get,
* when the library contains a function that return
* some kind of a struct, and then get its properties and values
**/
// Dispatch disp=Dispatch.call(comp,"sum",100,10).toDispatch();
// DataType Var=Dispatch.get(disp,"Property Name");
}
}
The output of this simple program:
[font="Lucida Console"]The Library been loaded, and an activeX component been created
Summation= 150
Subtraction= 50
Multiplication= 5000
Division= 2.0
More details about Jacob solution:
Hope that this article helps to have a basic idea about using the jacob and save time for some people,
please if you have any question, feel free to contact me and ask!
Good Luck everybody
Jacob_Test.zip (58.13K)
Number of downloads: 2579






MultiQuote




|