Accessing Multidimensional Java Arrays From a COM Client |
|
This example shows how to access a 2-dimensional Object array (i.e., Object[][]) returned by a Java method to a COM client.
PREREQUISITES
Create a folder c:\temp\package where we will store all the files needed to run this example.
Make sure that your classpath includes the current directory (.) and jintegra.jar, and that your path includes the jintegra bin folder and a Java SDK bin folder. Example:
STEPS
Create the Java Application
1. Download package.zip and extract the contents to c:\temp\package.
2. Look for the java file named CanadianCities.java and compile it.
Transform the Java Application to a COM server
1. Run Java2Com by typing java2com on the command prompt:
2. Set the java2com applet that pops up with the following parameters:
After clicking the generate button, Java2Com will generate Java wrapper files and an idl file named CanadianCitiesLib.idl. These files will be saved in c:\temp\package
3. Compile the Java wrapper files using javac IID*.java
4. Compile the idl file using midl CanadianCitiesLib.idl. Before doing this, make sure that you have exited from the java2com applet as the applet might still have a handle on the generated idl file causing a Permission Denied error.
The compiler midl produces a type library named CanadianCitiesLib.tlb. This is saved in c:\temp\package as well.
Create the JVM
1. Create a Java program that will run as a JVM. This JVM will host the COM server created in the previous step. Any client calls to the COM server will eventually be passed to and handled by this JVM. Look for the file HostJVM.java in c:\temp\package and compile it. This will be the JVM. If you look into the file, it performs two simple functions:
a) it registers and runs a JVM
b) sleeps/waits for calls to the COM server
Link the COM server and the JVM
1. Register the type library using regtlb:
This performs 2 functions:
a) registers the type library in the Windows registry (same function as regsvr32)
b) tells JIntegra that the COM classes defined in the type library are to be found in the JVM CanadianCitiesJVM
2. Register the JVM in the local machine:
regjvm registers CanadianCitiesJVM in local machine and that JVM will be listening on port 5252
Run the JVM
1. Run the JVM as follows:
Create and Run the COM client
1. Now that the JIntegra-enabled COM server is up and running, we can create a simple COM client that consumes the services of the COM server. Using Microsoft Visual C++ version 6.0, open the C++ workspace named CPPclient.dsw under c:\temp\package\CPPclient.
2. Compile and run the project. If everything went well, you should see an output similar to the following: