Overview:
This article practically describes how COM objects could be utilized in Java. The user should understand and have general programming skills in Java. This article also gives a small introduction of what COM is all about.
What is COM?
COM is a popular object model for creating software applications from independent components. The binary interfaces exposed by COM objects are platform-independent. Therefore, various facets of applications can be written in any COM-compliant language. COM objects also promote encapsulation and object reuse. For example, earlier functionality can be contained in COM wrappers and reused in Microsoft® Windows® Distributed internet Applications Architecture (Windows DNA) and other Web environments.
In simple words:
All the DLL files that are found in System32 folder of in your C:\windows directory resemble COM object model. Example: c:\windows\system32\shdocvw.dll. The “shdocvw.dll” file contains the type library for the Microsoft® Internet Explorer 4.0 COM object model.
This article does not provide information about COM in detail. If you are interested in knowing more about COM the best place is to search Google.
Required Software:

* Microsoft virtual machine (Microsoft VM)

* Microsoft SDK for Java 4.0 (Click here To Download)

While installing above said software accept the default settings such as location

Steps for calling COM object in Java:

Step 1:

Setting the “PATH” variable- I assume that you have installed your Microsoft SDK for Java 4.0 at “C:\Program Files\Microsoft SDK for Java4.0”. Right click “My Computer” select Propertiesà Advancedà Click on “Environment Variables”. Another window pops up. Click “New” button if Path is not seen or “Edit” button if Path variable is already available. Type variable name as PATH and variable value as:

“; C:\Program Files\Microsoft SDK for Java 4.0\Bin;” Now click Ok.

Path could also be set using the following command at the command prompt.

C :\> set path=C: \Program Files\Microsoft SDK for Java 4.0\Bin;

Setting “CLASSPATH” variable- The way “PATH” variable is set it’s the same for “CLASSPATH” also. The Variable value would be as:

“.;C:\windows\java\trustlib;”

To set the “CLASSPATH” at the command prompt:

C:\>set classpath=.;c:\windows\java\trustlib;
The above said process (not using Command Prompt) is used to reduce the overhead of specifying the path and classpath every time you compile and run a java program.

Step 2:

Generating Java-Callable Wrappers (JCW) - Java-Callable Wrappers are simple Java objects with additional class file attributes. The Microsoft VM uses Microsoft-defined attributes in Java classes to expose COM objects to Java applications. These attributes are typically generated by the Microsoft Java compiler, jvc, from @com directives in Java source files. The Java representation of COM co-classes and interfaces looks exactly like other Java classes and interfaces, except for the addition of @com directive comments above the class, interface, and method prototypes.

At the command prompt execute the following line:

C :\> jactivex c:\windows\system32\shdocvw.dll

If you encounter any error it’s because “PATH” variable is not properly set. Repeat step1 again if the error says jactivex is not a built in command or something like that.

JACTIVEX command exposes the COM component and generates JCW. A Java-Callable Wrapper just looks like any other java program written into a java file. To view a generated Java-Callable Wrapper check C:\windows\java\trustlib directory. Under shdocvw folder .java files are stored. This folder includes all the interfaces and classes a COM component has exposed.

We need to compile this folder with the generated .java files. To compile the folder containing .java files, Visual Java Compiler (jvc) is used.
Step 3:

Generating Class files of JCWs- The generated JCWs or simply .java files contain @com directives. Do not manipulate or delete them as they are commented. Class IDs are the ones with which the actual COM component is tracked. While generating class files of all these JCWs, care should be taken that these @com directives are also processed. We make use of Visual Java Compiler or jvc command to compile these JCWs. We make use of “/x-” switch to processes @Com directives while compiling these java files using jvc compiler.

At the command prompt execute the following line:

C:\>jvc /x- c:\windows\java\trustlib\shdocvw\*.java

Upon navigating to c:\windows\java\trustlib\shdocvw folder you will find that all the .java files are successfully compiled and .class files are generated.
Repeat step1 and append c:\windows\java\trustlib\shodocvw; to CLASSPATH variable.

Step 4:

Program to access COM object in java- This is a sample program that makes use of generated JCWs to print the URLs of the currently opened Internet Explorer.

/**

*Program can be modified according to your need.

*Author: Reuben

*Dated: Monday, September 24, 2007.

*/

import shdocvw.*;

import com.ms.com.Variant;

public class TryCom {

public TryCom() {

}

public static void main(String args[]){

IShellWindows sh= (IShellWindows) new ShellWindows ();

// Counts number of Windows currently opened.

System.out.println("Windows Currently Opened:"+sh.getCount());

Object tWin=null; //Temporary Window Object

for(int i=0;i

tWin=sh.Item(new Variant(i));

if(tWin instanceof IWebBrowserApp){

//Converting the obtained object into a browser.

IWebBrowser2 IE= (IWebBrowser2)tWin;

System.out.println("Window URL: "+IE.getLocationURL());

}

}

}

}

Save the file in the directory where ever you like to save. Let us Assume to have saved the sample program as TryCom.java at C:\COM directory.
Step 5:

Compiling Sample Program- To compile the sample program we make use of Visual Java Compiler or JVC. Execute the following statement at C:\COM>

C:\COM>jvc /x- /nomessage TryCom.java

Successful compilation would display some version details etc.

Step 6:

Executing Sample Program-JACTIVEX, JVC are the tools provided by Microsoft SDK for Java 4.0. JACTIVEX is used to generate JCWs. JVC is used to compile the generated JCWs or .java files.

To execute the class file of the sample program we make use of another tool provided by Microsoft SDK for Java 4.0. It is JVIEW. This is used in much the same way as java command used to execute normal java programs.

Execute the following statement at C:\COM>

C:\COM>jview TryCom

Windows Currently Opened:2

Window URL: file:///D:/JavaNotes/java_com2.htm

Window URL: file:///C:/WINDOWS/java/trustlib/shdocvw

This would eventually display sample output as shown above
Do write to me more of your ideas, suggestions and feedback.

Your code snippet and the instructions are really helpful. I followed exactly what you have mentioned in your article and im almost there but I am struck at the last point because I am not able to locate the package com.ms.* which is required for compling the code.

Can you please help me to find out the location from where I can download this package to compile my code?

I am trying to access a COM object via my Java applet but to achieve that I am not able to find this package which is the only pain point at the moment.

Your help will be appreciated. Thanks in advance.
-Gurdev

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.