0% found this document useful (0 votes)
2 views5 pages

Jacob Example

This article explains how to use DLL libraries in Java applications using the JACOB API, which allows Java to interact with COM components. It provides a step-by-step guide on loading the JACOB library, registering the DLL, and calling its functions within a Java program. The article also addresses potential issues with class loaders in servlet environments and offers a solution for proper integration with Tomcat.

Uploaded by

clsree08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Jacob Example

This article explains how to use DLL libraries in Java applications using the JACOB API, which allows Java to interact with COM components. It provides a step-by-step guide on loading the JACOB library, registering the DLL, and calling its functions within a Java program. The article also addresses potential issues with class loaders in servlet environments and offers a solution for proper integration with Tomcat.

Uploaded by

clsree08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link] file and the
[Link] 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 [Link] 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 [Link] 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 [Link] file:


Just paste the [Link] 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
[Link] using regsvr32 command.

Register the dll file “the Library”


To register the dll file, we use the “regasm /codebase” command from the [Link]
either from the windows command prompt, or from the .Net command prompt itself as
follows:
C:\WINDOWS\[Link]\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 [Link] 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: [Link]
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 [Link] 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 [Link] file to your class path; the following is the code segments of needed
operations for using the Library:

First load the library:


1 //Loading the library:

2 ActiveXComponent comp=new ActiveXComponent("[Link]");


[Link]("The Library been loaded, and an activeX component
3
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:


01 int summation=[Link](comp, "sum",arg1,arg2).toInt();

02 [Link]("Summation= "+ summation);

03

04 int subtraction= [Link](comp,"subtract",arg1,arg2).toInt();


05 [Link]("Subtraction= "+ subtraction);
06

07 int multiplication=[Link](comp,"multi",arg1,arg2).toInt();
08 [Link]("Multiplication= "+ multiplication);

09

10 double division=[Link](comp,"div",arg1,arg2).toDouble();
11 [Link]("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:
1 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 [Link] follows:


01 package jacobtest;

02

03 import [Link].*;

04 import [Link].*;

05 /**
06 * This class uses the the Jacob tech
07 * to use and interact with a Com cmponent
08 * in a java application

09 */

10 public class ReadDLL {

11

12 public static void main(String[] args){


13
14 //Loading the library:
15 ActiveXComponent comp=newActiveXComponent("[Link]");
[Link]("The Library been loaded, and an activeX
16
component been created");
17

18 int arg1=100;
19 int arg2=50;
20 //using the functions from the library:
21 int summation=[Link](comp, "sum",arg1,arg2).toInt();
22 [Link]("Summation= "+ summation);
23

24 int subtraction= [Link](comp,"subtract",arg1,arg2).toInt();


25 [Link]("Subtraction= "+ subtraction);
26

27 int multiplication=[Link](comp,"multi",arg1,arg2).toInt();
28 [Link]("Multiplication= "+ multiplication);

29

30 double division=[Link](comp,"div",arg1,arg2).toDouble();
31 [Link]("Division= "+ division);
32
33 /**The following code is abstract of using the get,
34 * when the library contains a function that return
35 * some kind of a struct, and then get its properties and values
36 **/
37 // Dispatch disp=[Link](comp,"sum",100,10).toDispatch();
38 // DataType Var=[Link](disp,"Property Name");
39 }
40 }

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!

thank you for recognizing me.

i have found solution to this problem..


for your knowledge ,let me explain
in jacob package ,,there are two things([Link] and [Link]) causing problems if they
are misplaced. The [Link] loads the [Link].

when in new process ,the [Link] reference the [Link] than in that procees the
[Link] loaded once. in that process next call to load [Link] will not load the [Link]
again but will not do loading because in one process the same class loader will not load
the same dll.

where as in servlet issue . for each servlet there may be different classloaders in one
process . diffrent classloaders loadind same dll may cause problem..
and solution is ,to use single classloder to load class loading the [Link].
thus perticulary for tomcat 6.0. In netbeans make javaclasslibrary project that has
access to [Link] that loads the [Link] .place this([Link]) in C:\Program
Files\Apache Software Foundation\Tomcat 6.0\lib folder.(completely deploy including
dist folder).

now no need to add reference of [Link] in your servlet project because your
[Link] refered by tomcat 6.0.

thus directly use your [Link] to acceess [Link] from your servlet project, so as
every time classes in [Link] are loaded by same classloader of tomcat6.0. and
therefore for each servlet ,[Link] is loaded only once.

my motto is truth works. God bless you all.

You might also like