Visual Basic Accessing Java via Late Binding |
This example demonstrates how to access Java objects from a Visual Basic EXE.
From VB there are two ways of accessing Component Object Model (COM) objects: early binding access and late binding access. Late binding access means that there is no information available at compile time about the object being accessed -- everything is evaluated dynamically at runtime. This means that it is not until you run the program that you find out if the methods and properties you are accessing actually exist.
Late binding access is slower than early binding access, and since there is no information available about the objects being accessed, more prone to user errors. However it does provide an enormous amount of flexibility, and is extremely easy to set up, as this example will show. You may wish to move to the Early Binding example after finishing this example.
For more information about comparing early binding and late binding, please refer to Microsoft article Using Early Binding and Late Binding in Automation.
In this example you will compile and run a simple Java class, and then access it from Visual Basic using late binding. We initially assume that the Java class will run on the same machine as the Visual Basic client, but later we explain how it can run on a totally different machine, such as a UNIX machine.
The J-Integra® must be installed on both the Windows VB client, and on the machine running the Java class being accessed (more precisely, the J-Integra® bin directory is required on the Windows client, and the J-Integra® runtime (jintegra.jar) is required on the machine running the Java class).
Follow the steps below to see how VB client accesses Java objects on local machine.
This example assumes that you have installed
the JDK under C:\jdk1.3.1
C:\jintegra
We will be performing this example under D:\pure\simple
.
Create that directory. Set your PATH
environment
variable to include the JDK and J-Integra® bin directories, and update
your CLASSPATH
environment to include the J-Integra® runtime and
the Java objects you need to access from VB. In the example below, the Java
objects are D:\pure\simple:
Compile the example Java class
This is a simple Java class which will be accessed from Visual Basic.
It demonstrates that all public fields and methods, including static fields
and methods are accessible:
public class Simple { public static void main(String[] args) throws Exception { com.linar.jintegra.Log.logImmediately(3, "jintegra.log"); // Register the JVM with the name "firstjvm" com.linar.jintegra.Jvm.register("firstjvm"); Thread.sleep(6000000); // Sleep for an hour } public int property1; public static java.util.Date property2; public void method1(String aString) { System.out.println("method1 has been called with " + aString); } public static void whoops() { Object o = null; o.toString(); // Whoops } } |
Cut and paste the above class from your Web Browser into the
Simple.java
file, and then build it using the javac Simple.java
command:
Pick a Free Port
Displays protocol statistics and current TCP/IP network connections to make sure whether a port number is free and not used by any other applications:
netstat -a
Register the JVM
There are three modes that you can use to register the JVM: DCOM mode, native mode out-of-process, and native mode in-process. This example shows the DCOM mode. Check out the regjvm tool for more information.
Register the JVM using the regjvmcmd firstjvm localhost[1350]
command:
This tells J-Integra® that it can find the classes in the JVM 'firstjvm' by connecting to TCP/IP port 1350 on the local host.
Start the JVM, using the java -DJINTEGRA_DCOM_PORT=1350 Simple
command:
This tells the J-Integra® DCOM engine to try to listen for incoming DCOM calls on TCP/IP port 1350.
The Simple.java which invokes com.linar.jintegra.Jvm.register("firstjvm") is not needed if you register JVM in native in-process option.
Here is a trivial VB program which exercises the Simple Java class.
Start Visual Basic, create a 'Standard EXE" project, put a
button on a form, and double-click on the button to edit its action code:
Private Sub Command1_Click() |
When you run the program and click on the button you should see the following
series of messages boxes:
The JVM displays a message when method1 is invoked, and also displays a stack trace when the whoops method deliberately causes an exception:
As you see, J-Integra® propogates Java exceptions to the VB environment.
There is nothing to stop you from running the JVM on a UNIX machine (or any other machine that supports a standard JVM, and has TCP/IP configured).
Copy the Simple.class file and jintegra.jar file to a UNIX box. Make sure
both Simple.class and jintegra.jar are included in the CLASSPATH environment
variable on the UNIX machine. Run it as above (java
-DJINTEGRA_DCOM_PORT=1350 Simple
).
On the Windows client, unregister the JVM:
Assuming the UNIX machine running the JVM has the TCP/IP host name mymachine.mycompany.com,
re-register the JVM on the Windows VB client machine using the
command regjvmcmd firstjvm mymachine.mycompany.com[1350]
:
Try running the VB client again.