Java/COM Language Integration

The integration of Java and COM can be achieved by just making changes to the Java VM, and not adding any new keywords or constructs to the Java language. That is, the Java language already has the constructs that allow for the implementation and use of COM objects. In particular, Java, like COM, supports the notion of multiple interfaces on an object.

Key requirements for Java-COM integration include ease of programming and size/performance. Developing and using COM objects in Java is really no different than developing and using Java classes. The Microsoft Java implementation is also designed to be very efficient with minimal overhead per COM class created with Java.

The Java language has a certain philosophy related to it, and Microsoft does not wish to modify the language arbitrarily to integrate with COM in such a way that would be against the grain of that philosophy. Thus there are things that are possible with COM in C/C++ or other languages which are not possible with Java. Specific examples include restricting aggregation to a single aggregated object and a restrictive set of types that can be used in COM interface definitions. Appendix: Java/COM Language Integration Details contains a detailed discussion of exactly how COM and Java are integrated at the language level by Microsoft's Jakarta compiler.

Note Throughout this paper there are examples of Java source code illustrating how COM and Java are integrated. These examples use compiler features which will be found in Microsoft's Java compiler, codenamed Jakarta. It is important to note that the details of the language integration is completely up to the compiler, and other vendor's Java compilers may expose Java/COM integration differently. However, the way that the compilers produce Java .class files is standardized. This paper does not directly discuss the details of Microsoft's proposed .class file attributes for COM integration.

The following example illustrates how COM and Java are integrated showing how a COM object, which implements a simple "hello world" interface, is implemented in Java. Assume that the IHelloWorld interface is declared in a IDL (Interface Definition Language) file as:


[object, uuid(1F090040-9B7B-11cf-B63C-0080C792B782)]
interface IHelloWorld : IUnknown
{
	HRESULT SayHello();
};

[uuid(1F090041-9B7B-11cf-B63C-0080C792B782)]
coclass Hello
{
	interface IHelloWorld;
};

If the above IDL file were compiled into a COM type library named hello.tlb, the following Java code would provide an implementation of the hello COM class.


import samples.hello.*;
class Hello
	implements IHelloWorld
{
	void SayHello(void)
	{
		System.out.println("Hello world!");
	}
}

COM client code, written in any programming language could then create an instance of the Hello class and invoke the SayHello method. For example, the following C++ code does just this:


#include "hello.h"  // defines IHelloWorld and CLSID_Hello
IHelloWorld* pHello;
if (SUCCEEDED(CoCreateInstance(CLSID_Hello, NULL, 
				CLSCTX_SERVER, IID_IHelloWorld, (void**)&pHello)))
{
	pHello->SayHello();
	pHello->Release();
}

As is evidenced by this example, developing COM objects in Java is no different than developing standard Java objects. In fact, all Java objects automatically are COM objects.

But what about using COM objects in Java? Just as all Java objects are COM objects, all COM objects appear to Java programmers as normal Java objects. To illustrate, here's some Java code which causes a Microsoft PowerPoint™ presentation to be run as a slide show:


powerpnt.Presentation pres = ActiveX.moniker.BindToObject("slides.ppt");
powerpnt.SlideShow show = pres.SlideShow();
show.Run(ppSlideShowFullScreen);

In this example a moniker was used to bind to a PowerPoint™ presentation. The ActiveX.moniker Java package is part of the classes included with Jakarta(1). Instead of using a class library function to activate a COM object, the Java new operator can be used as well:


powerpnt.Presentation pres = new powerpnt.Presentation();
...

Here, a new, empty presentation was created instead of loading an existing presentation.

There are several ways activate any COM object (even one implemented in Java!) remotely. One way is to use the COM library API's as exposed by the ActiveX Java class library:


samples.Hello.Hello hello = null;
hello = ActiveX.COM.CreateInstance(CLSID_HelloWorld, null, CLSCTX_REMOTESERVER, "132.22.122.32");
hello.SayHello();

The example above will activate the COM object CLSID_Hello (which, in our example, was actually built with Java) on the remote machine identified with the IP address given. When the SayHello method is invoked, it will actually execute on this remote machine, and the message ("Hello World!") will be printed on a console on the remote machine.

As evidenced by the preceding series of examples, the integration of Java and COM is very tight. But most importantly, it is very natural to Java programmers.

© 1996 Microsoft Corporation