Sahir Shah
November-2002
Every framework has it's weaknesses and strengths. For example Delphi's VCL has excellent database access components, Visual Basic is good for creating a good looking user interfaces, Java's class library is very strong on networking etc. So, it makes more sense to develop projects using mutiple languages and class libraries, than to religiously stick to one development tool. If you are largely using Visual Basic, Visual C++ or Delphi and you run into a particular task that is difficult to achieve in these environments and is much simpler in Java, dont let the percieved difficulty in accessing Java code from other languages deter you. It is really a very simple process to call Java code from Visual Basic, Delphi , C++ etc.
All you have to do is convert your Java class to a bean and use the ole packager (which comes with the Java SDK) to convert the bean to a COM component. Let's take the following Java class as an example :
package com.sahir;
public class BeanTest{
public int multiply(int a, int b) {
return a * b;
}
public int add(int a , int b) {
return a + b;
}
}
The package declaration package com.sahir; is important because it is essential for this class to be part of a package. The naming convention for packages is, your internet domain name, followed by the path of your package hierarchy. For example if I wanted to create a package called util, I would create it as com.sahir.util. This naming convention prevents a clash between class names. For example if I had created a class called HashMap, I would create it as a member of com.sahir.util. This naming convention helps to distinguish it from another class with the same name. My class would be referred to as com.sahir.util.HashMap which is distinct from java.util.HashMap or any other class called HashMap.
Set the class path on your machine as c:\java. This is the root of your classpath. Now save the
source file as c:\java\com\sahir\BeanTest.java and compile it to create the class
file (please replace sahir with your name in the package declaration and
the file path). Now using notepad or some other text editor create a file which
contains the following text.
Name: com/sahir/BeanTest.class
Java-Bean: True
Save the file as c:\java\Meta-inf\Manifest.MF. This is your manifest file. A manifest file
lists the contents of the JAR file. In this case it also identifies the class BeanTest as a bean.
Now run the JAR utility to create a JAR file. Run cmd.exe and make c:\java your current directory.
Now type in the following:
jar -cvfm beantest.jar META-INF\MANIFEST.MF com\sahir\BeanTest.class
or alternately use a batch file to run the command. If there is as much as as an
extra whitespace in the manifest file the jar utility will throw an error, so
ensure that the manifest file is exactly as shown here. There
shouldn't be any trailing spaces at the end of the file and there should be no
space between Name and the : separator and ensure there is exactly
one space after the : and the beginning of the class name. Running the JAR
utility will create a JAR file called beantest.jar in c:\java.
Now
you need to start the packaging utility class Packager.class, which can be found
in sun.beans.ole. This class can be found in a jar file called jaws.jar in
the lib folder of your Java SDK installation. If your JDK installation is
in d:\jdk1.3 then you can start the packager class by typing in
java -classpath d:/jdk1.3/jre/lib/jaws.jar sun.beans.ole.Packager
This starts the packager utility application.
![]() |
Select the jar file you created and click next.
![]() |
If you had not used a package name you would not be able to progress beyond this stage. The packager will not permit a class in the default namespace [.] to be converted into a bean.
![]() |
Using the class name as the name of your activeX control is the logical thing to do, however it is not mandatory, you may choose a different name here. When you finish running the packager utility it will create a type library and a .reg file in the target directory. Double click the registry file to register the activeX component. After rebooting your machine the bean can be accessed as an activeX component from Visual Basic , Delphi or Visual C++.
To test this, open Visual Basic and create an empty project. Select Project|References from the menu bar. You wil be able to see our BeanTest control among the list of available activeX components.
![]() |
Add the BeanTest component to your project. Drop a button on the form and add the following to the buttons on_click method.
Private Sub Command1_Click()
Dim bt As New BeanTest.BeanTest
MsgBox (bt.Add(2, 2))
End Sub
Run the project and click the button. Nothing happens for a long time !!! This is normal.
It's because the
Java virtual machine has to be loaded into memory before the bean can do it's
stuff. Once the virtual machine is loaded up and ready to run you will get
a normal response from the bean. You can verify this by clicking the button a
second time. You can see that the response is much faster.