java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JNI arrow Wrap a Java beans in a COM object
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4084
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Wrap a Java beans in a COM object E-mail
User Rating: / 10
PoorBest 

In a previous tip, we used 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,    _
       "JAVA BEAN OUTPUT"
       
objJava.setHello("Bonjour le monde!")

strFromJava = objJava.getHello
MsgBox strFromJava, _
       0,    _
       "JAVA BEAN OUTPUT"

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.