Java Using a COM Component

This article has four sections:
To see:

Overview of the sample

The JavaCallingCOM sample demonstrates Java calling COM. Like JavaBeep, it uses services from a simple Automation server. This Automation server includes properties, as well as methods. In this sample, you register the automation server COMSERVER.DLL. You also create a Java description of this server using the Java Type Library Wizard.

About the server

COMSERVER.DLL is a dual-interface in-process automation server, like BEEPER.DLL, which is used in the JavaBeep sample.

The comserver component has two properties and one method. It uses an enumeration type.

About the sample

The sample class usecom uses the constants in BeeperConstants and the SoundName property to create a list of names and values. This list is used to populate the list box in the applet, and also to map the users selection in the list box to a sound for comserver to play. This code is located in the usecom.init method. The handleEvent method is the standard way for applets to respond to events from any components they contain. In this applet, in response to a change in the selection in the list, handleEvent sets the Sound property of comserver and echos the numeric value in a label component. In response to a press event from the button, handleEvent calls the comserver.Play method.

About security

COM components can access any system resources. As such, they are both very powerful and potentially very dangerous. In the Java virtual machine (VM) in Microsoft Internet Explorer 3.0 only trusted class files can use COM components. Class files from digitally signed CAB files are trusted.

If the HTML file is run from Microsoft Developer Studio, the class files are also trusted. This can be very helpful during applet development. However, to deliver your applet to other users, you must put it in a signed CAB file.

When run as an application, the class file must already be on your machine. When run as an applet, the class file may be downloaded to your machine. There is a potential security threat running downloaded software. To run a Java applet that uses COM components, the applet code must be trusted. For more information on trusted class files, see the CabAndSign sample.

Return to the top of this article.


How to use this sample

To run the JavaCallingCOM sample:

  1. Register COMSERVER.DLL, the COM component for this sample.
    See
    Registering a COM Component.
  2. Create a Java interface for the COM component.
    See Creating a Java Description for a COM component.
  3. In Developer Studio, build the applet.
  4. In Developer Studio, run the applet.
    If this is the first time you have run the applet, Developer Studio will prompt you for the name of the class. Type usecom.
Return to the top of this article.

Troubleshooting this sample

If you get the error message java.lang.VerifyError:

If you get the error message ClassNotFoundException:

If you get the error message UnsatisfiedLinkError:

Return to the top of this article.

Files in this sample

This sample includes the following files.

Project files:

Support files: Return to the
top of this article.