Example 1 - Setup - Kishore J Doshi
Download All files Associated with this example in two ZIP files
All Java Source Code
COM HelloServer Visual Studio Project
Section Links
Back to Example 1 Description
Screenshots for Example 1
Back to Section Introduction
The user must download the Microsoft SDK for Java 3.1 including the new Microsoft VM to do this example, get them here. You download an active setup program which will present you download and install options, choose Download and Install from Internet. The active setup program sets the install path to c:\SDK-Java.31
Make sure you select the options to download the update to the Virtual Machine and the SDK documentation when the setup program asks you what components you want to download
Once the setup is complete, put the SDK Bin directory on your PATH(c:\SDK-Java.31), this way you can call the tools on the command line, from any working directory
Creating the COM Server:
Creating this server is simple and I will not get into the details here since that is not the purpose, but here are the steps:
Using Microsoft Visual C++ 5.0 - run the ATLCOM Wizard and create an Automation Object
Modify the IDL file, and add the HelloWorld interface to the object, the interface has one method. Look at the IDL file here.
The HelloWorld method in IDL: HRESULT HelloWorld([out, retval]BSTR * helloString)
Provide an C++ implementation for HelloWorld. Look at it here.
Build the .exe for the server. The compiler will register the server and create the HelloServer.tbl file we need to create the Java class for the server.
The user does not need to create the server project from scratch. I provied the the files for the Visual Studio project in the zip file here.
The user only needs to compile the project and let the compiler register the server for you
Creating the Java Client:
Any classes we create for COM objects must be on the Trusted ClassPath of the VM to load. I suggest doing the following:
Create a directory on the root level of your drive called compackages, let's assume it's c:\compackages
Assuming you have installed the Microsoft Virtual Machine, we must modify the registry settings
Start regedit(registry editor) and goto HKEY_LOCAL_MACHINE/Software/Microsoft/Java VM
Look at the key named TrustedClasspath, edit this and add the compackages directory. So if you put the compackages directory on the C drive root, you put c:\compackages as the value of the TrustedClasspath key.
Run JActiveX on the HelloServer.tbl file. The syntax is: jactivex /javatlb /d c:/compackages HelloServer.tbl Remember that your working directory must contain the file HelloServer.tbl, which is in the HelloServer.zip file you downloaded in the section above.
The JActiveX tool creates a subdirectory in c:/compackages called helloserver. It takes the name of the .tbl file. In there you will find it created three files (Note: You can also just create the directory and retrieve the files listed below from the zip file containing the source code):
ComHelloServer.java
IComHelloServer.java
IComHelloServerDefault.java
Create another new directory on your C Drive root, call it JavaComClient
Download the client driver source code to the JavaComClient directory: Client.java
Now were ready to compile the java files. Set you current working directory to be JavaComClient and call the Java compiler with this syntax: jvc /x- Client.java c:\compackages\helloserver\*.java. The /x- tells the compiler to use the extensions. This is necessary to use the @com extensions in the ComHelloServer.java and IComHelloServer.java files. We also specify the directory with the Java files for the COM HelloServer Object(c:\compackages\helloserver) as they need to be compiled also.
Running the Java Client
This is the easiest part, assuming you have compiled successfully, you have the following class files in the JavaComClient directory
CallComFunction.class
ClearDisplay.class
Client.class
Client$1.class
WindowHandler.class
and in the c:/compackages/helloserver directory
ComHelloServer.class
IComHelloServer.class
IComHelloServerDefault.class
Make sure the CLASSPATH environment variable contains the JavaComClient directory.
Run the Client, the syntax is Jview Client. The VM starts up the COM server if it is not running. Check the Task Manager and you should see a process for HelloServer.exe.
Your now using a COM service in a Java application, fairly painlessly!