Share this page 

Wrap a Java beans in a COM objectTag(s): Environment

The previous How-to was using a Microsoft utility to enable access to Java objects from a COM-aware development tool. Sun provides a similar tool but you must package everything in a jar file and use the Beans technology. The tool is called packager, written in Java, you execute it from the sun.beans.ole package. The Java Plug-in 1.2 and the JDK1.2 must be installed on the system (for download, see Java Sun Web site).

Let's try it with this simple class :

package JavaCom; 
 public class JavaBeanSays { 
   private String _hello = "Hello World!";
   public String getHello() {
    return _hello ;   
   public void setHello(String s) {
    _hello = s;
NOTE: This is not really a Bean but let's keep it simple!

The next step is to build a manifest file to identify the bean in the jar. Here it is (manifest.txt):

Name: JavaCom/JavaBeanSays
Java-Bean: true
NOTE: If no manifest is present all classes in the jar are treated as beans.

The JavaBeanSays class is in the directory JavaCom, the manifest.txt is the directory under it. From the directory under (the one containing manifest.txt), we built the jar with :

jar cfm javacom.jar manifest.txt javacom\JavaBeanSays.class
NOTE: You can download my JavaCom.jar if you to proceed more rapidly.

The next step is to run the packager. You run it from the JDK installation directory. If the JDK is installed in c:\dev\java\jdk1.2.1\ for example , you go there. And you start the packager with

bin\java.exe -cp jre\lib\rt.jar;jre\lib\jaws.jar sun.beans.ole.Packager

A wizard is started, you follow the 5 steps to create the "JavaBeans bridge for ActiveX" for the JavabeanSays component.

The first step is to specify where is located the JavaCom.jar file. When selected, the wizard should list the JavaCom.JavaBeanSays bean, press Next. The "ActiveX" name under which the beans will be seen is shown, press Next (in VbScript, the beans suffix must be added to this name).

An output directory is needed, be careful because this directory name will be hard-coded in the generated files (REG and TLB), you need to specify a valid directory name. The packager assume that a subdirectory bin is present with the file beans.ocx in it. You can create it and then copy beans.ocx from the JRE\bin into it or edit the REG file to specify the original JRE\bin and update the registry with the good location.

The Bean is now registered and ready to be used as a COM object.

NOTE: There is a command-line interface available in the packager if you want to bypass the wizard.

To test it, try this VbScript (TestJavaBeansSays.vbs)

'  VBSCRIPT  connect to a Java Bean
Dim objJava
Set objJava = WScript.CreateObject("JavaBeanSays.Bean")

strFromJava = objJava.getHello
MsgBox strFromJava, _
       0,    _
objJava.setHello("Bonjour le monde!")

strFromJava = objJava.getHello
MsgBox strFromJava, _
       0,    _
NOTE: Check the JAVA PLUG-IN SCRIPTING documentation (jdk1.2) or (jsdk1.4). document for more infos.