More About Java and COM Integration |
![]() Previous |
![]() Introduction |
![]() Index |
![]() Next |
The integration of Java and COM can be achieved by just making changes to the Microsoft VM and not adding any new keywords or constructs to the Java language. That is, the Java language already has the constructs for the implementation and use of COM objects. In particular, Java, like COM, supports multiple interfaces on an object.
Key requirements for Java/COM integration include ease of programming and size and how it relates to performance. Developing and using COM objects in Java is similar to developing and using Java classes. The Microsoft VM implementation is also designed to be very efficient with minimal overhead per COM class created with Java.
The Java language has a certain philosophy. In keeping with that philosophy, Microsoft is not modifying Java for COM integration. As a result, things are possible with COM in C and C++ or other languages that 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. For a detailed discussion of how COM and Java are integrated at the language level, see the Java-COM Language Integration Details section later in this article.
Note This article has examples of Java source code, illustrating how Java and COM are integrated. These examples use compiler features that are in the Microsoft® Java compiler (JVC). The details of the language integration is completely determined by the compiler. Java compilers from other vendors may expose Java-COM integration differently. However, the way that the compilers produce Java.class files is standardized. This article does not directly discuss the details of the Microsoft proposed .class file attributes for COM integration.
The following example illustrates how Java and COM are integrated. It shows how a COM object, which implements a simple "hello world" interface, is implemented in Java. Assume that the IHelloWorld interface is declared in an IDL (Interface Definition Language) file.
[object, uuid(1F090040-9B7B-11cf-B63C-0080C792B782)] interface IHelloWorld : IUnknown { HRESULT SayHello(); }; [uuid(1F090041-9B7B-11cf-B63C-0080C792B782)] coclass Hello { interface IHelloWorld; };
If the previous IDL file was 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 so:
#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(); }
The previous example shows that developing COM objects in Java is the same as developing standard Java objects. All Java objects automatically are COM objects.
© 1997 Microsoft Corporation. All rights reserved. Legal Notices.