JActiveX

Making an ActiveX Control Look Like a Bean

By Piroz Mohseni

The computer industry is filled with competing technologies. As implementers of these technologies, we’re often faced with a basic question: which one do I use? Sometimes competing technologies are very similar if we only consider their technical merits. In these situations non-technical factors usually are the deciding factors. These non-technical factors can be related to cost, investment in infrastructure, partner relationships, etc.

Two technologies that fall into the “competing” category are JavaBeans and ActiveX — component technologies from Sun and Microsoft respectively. From a technical standpoint, they have similar goals and try to address the same issues in software development, e.g. object-orientation, reusability, etc. The marketing engines, however, certainly don’t convey this message and instead promote them as competing technologies.

Developers are often faced with a decision as to which component technology to adopt. That decision is often an important one because the component model is a key element of the infrastructure upon which the rest of the software development process is based. There are several factors that play a role in such decisions, but if we can focus on just the technical reasons, we can often find ways to make competing technologies work together. This way, you still have to make a decision, but you aren’t locked into your choice, and can integrate one solution with another. This should take some of the pressure off the decision making process.

In this article, we discuss an approach for making ActiveX controls work within Java. We specifically discuss a tool from Microsoft — the Java/ActiveX Integration Utility (or JActiveX) — that produces Java code as a wrapper for an ActiveX control. It should be mentioned that this is only one way of bringing JavaBeans and ActiveX technologies together. Technologies exist to do the reverse as well, i.e. use Java code inside an ActiveX control.

Background

ActiveX is an established component technology based on COM. It’s a subset of COM for reusable and light components to be deployed on the Web. JavaBeans is a component technology for reusable Java programs. They can be deployed over the Web or as part of a Java application. Both technologies offer a framework for software reusability.

As systems come together, we often face a situation where two applications use two different technologies, and our task is to find a way to bridge the gap. Because of its age, and the fact that it’s a Microsoft standard, COM is more established than JavaBeans. There are many COM-based controls/components, and most of them can be easily modified to be ActiveX controls. Much time and effort has gone into developing these controls. Java is a relatively new player in the enterprise. It offers some enhancements over existing technologies, however, so it’s an attractive choice for many enterprise applications due to its portability, object-orientation, and other language features.

As you invest in Java, at some point, you have to ask yourself about existing systems and how they would fit into the Java model. One obvious option is rewriting, which is expensive and time-consuming. Another option, which is more attractive, is to wrap older components with a layer that allows them to interface with the new system. This wrapping technique (see Figure 1) has been used successfully to integrate systems that at first seem impossible to coexist, and it very well have its roots in the encapsulation model advocated by object-oriented design.

Figure 1: Wrapping allows seemingly different technologies to co-exist.

JActiveX is a command-line tool that allows you to use an ActiveX control in a “pure” Java program, i.e. it makes an ActiveX control look like a JavaBean. If you’ve used the command-line tools javatlb or jcom, JActiveX should look familiar; it’s the official replacement for those tools.

Creating an ActiveX Control

Let’s follow the steps to include an ActiveX control inside a Java program. First, we’ll need an ActiveX control, so let’s develop a simple one using Visual Basic 5 (VB5). The ActiveX control will contain a button and a label. Each time you click the button, a “random” number between 0 and 10 will be generated and displayed by the label. The ActiveX control and all other binaries and source discussed in this article, are available for download; see the end of this article for details.

To create the control from scratch, start VB5 and select ActiveX Control from the New Project dialog box. This will create an empty UserControl object (see Figure 2). Rename it SimpleX in the Properties window, i.e. replace “UserControl1” with “SimpleX” as the Name property.

Figure 2: Creating the example ActiveX control using VB5. Selecting ActiveX Control from the New Project dialog box creates an empty UserControl object.

In the Toolbox, click on the CommandButton icon, then click and draw the outline of the CommandButton on the UserControl. Set its Caption property to Random, and rename it myButton. Repeat the process using a Label control, deleting its Caption property and renaming it myLabel. Position and resize the objects until they resemble Figure 3.

Figure 3: Applying finishing touches to the ActiveX control in VB5.

Double-click the CommandButton control. VB5 will display a Code window for the control with an empty myButton_Click event-handler method already created for you. Enter the VB code as shown in Figure 3.

Now select File | Save Project As to save the control as SimpleX.ctl, and the project as SimpleX_Project.vbp. Finally, select File | Make SimpleX_Project.ocx to display the Make Project dialog box (see Figure 4). Change the File name to SimpleX.ocx and click OK to create the .ocx file.

Figure 4: The VB5 the Make Project dialog box.

That’s all there is to it; we’ve created an ActiveX control. Now we need to register it with the following command:

regsvr32 SimpleX.ocx

as shown in Figure 5. With this accomplished, we can focus on converting the control into Java.

Figure 5: Registering the ActiveX control.

Using JActiveX

Now that the control exists as an .ocx file, we can use the JActiveX tool. JActiveX is shipped as part of the Microsoft SDK for Java. You can find it under the \bin directory of your SDK installation. (JActiveX doesn’t come with Visual Studio or Visual J++, so you must download the Microsoft SDK for Java to obtain it. The latest version, 3.1, is available for download at http://www.microsoft.com/java/download.htm.)

Now open a Microsoft-DOS prompt window and enter and run the following command:

jactivex simplex.ocx

This command causes a directory to be created under \Windows\Java\Trustlib with the same name as the control — \simplex in our case. It also creates six Java files in that directory (see Figure 6).

Figure 6: The JActiveX utility creates these files in the /Java/TrustLib/simplex folder (they’re listed in Listing Nine).

JActiveX features a number of command-line options. One I found useful is the /l option. e.g.:

jactivex simplex.ocx /l SimpleX_Files.txt

This command will not only create the six Java files as before, it will also create text containing a list of the Java files. In our case, it’s named SimpleX_Files.txt and looks like this:

C:\WINDOWS\java\trustlib\simplex\__SimpleX.java
C:\WINDOWS\java\trustlib\simplex\SimpleXEventMulticaster.java
C:\WINDOWS\java\trustlib\simplex\SimpleXEventListener.java
C:\WINDOWS\java\trustlib\simplex\SimpleX.java
C:\WINDOWS\java\trustlib\simplex\SimpleXRaw.java
C:\WINDOWS\java\trustlib\simplex\_SimpleX.java

This separate file is useful when you need to compile the Java files. The file names tend to be lengthy, so the contents of this file come in handy for cutting and pasting to save time and avoid typos.

These six files collectively provide all the code necessary to use the SimpleX control from a Java program. They’re shown in their entirety in Listing Nine, beginning on page xx.

Notice that the files all include the following statement:

package simplex;

which effectively puts the files into a single package.

Also note how the constructor for the SimpleX class is included in SimpleX.java. There are also methods to handle the Java 1.1 event model, which is based on event listeners. The other files complement SimpleX.java and, depending on the ActiveX control, they could become lengthy.

You’ll also see @com directives embedded in comments in some of the files. These are not ordinary comments; they mean something to the Java compiler. They also mean that not all compilers will be able to compile the Java files generated by JActiveX. You need version 1.02.3920 or later of the jvc compiler. (The jvc is also part of the Microsoft SDK for Java.)

After generating the Java files, you can compile them to create a set of class files that can be used in your Java program. For example, this command:

jvc simplex

creates a class file named SimpleX.class from the SimpleX.java file. (If you wish, you can package the class files along with a manifest file in a JAR file as JavaBeans.)

Once you’ve created the class files, you can execute the following command as a quick-and-dirty way to see the Java version of the control:

jview /a simplex/SimpleX

Note that we have to precede the name of the class, SimpleX, with the name of the package, simplex.  The package is created as part of the JActiveX command. You will probably have to resize the JView window to see the control inside of it. Figure 7 shows the ActiveX control running inside the JView tool.

Figure 7: The SimpleX control running inside the JView tool.

Conclusion

We’ve discussed a few aspects of the JActiveX tool, a utility program that provides a systematic mechanism for wrapping ActiveX controls so they can be used inside a Java program. The wrapping technique allows you to use the large collection of ActiveX (COM) controls that already exist in the market, and integrate them with your Java applications that will run on Windows. In other words, you get the best of both worlds.

The Visual Basic and Java source files referenced in this article are available for download from the Informant Web site at http://www.informant.com/ji/jifile.asp. File name: ji9901pm.zip.

Piroz Mohseni is a Senior Managing Consultant with Automated Concepts Incorporated (ACI) in New York. He specializes in enterprise Java and database applications. Contact him at piroz.mohseni@autocon.com.