Visual Basic Accessing Java via Late Binding

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access Java objects from a Visual Basic EXE.


  1. Introduction
  2. VB client accessing local Java objects
    1. Configure your environment
    2. Compile the example Java class
    3. Register the JVM
    4. Start the JVM
    5. Run the VB Client
  3. VB client accessing remote Java objects on UNIX, Linux, Windows and etc

1. Introduction

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).

2. VB client accessing local Java server

Follow the steps below to see how VB client accesses Java objects on local machine.

2.1 Configure your environment

This example assumes that you have installed

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:

2.2. Compile a Java class

  1. 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"
        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 file, and then build it using the javac command:

2.3 Register the JVM

  1. 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

  2. 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.

2.4 Start the JVM

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 which invokes com.linar.jintegra.Jvm.register("firstjvm") is not needed if you register JVM in native in-process option.

2.5 Running the JVM on remote machine, e.g. UNIX, Linux, Windows and etc

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()

Dim simple As Object

' To use CreateObject, use regprogid to register a progid first.
' For example: run regprogid firstjvm.Simple "firstjvm:Simple"
' Then you can invoke Set simple = CreateObject("firstjvm.Simple", "") from VB
Set simple = GetObject("firstjvm:Simple")

MsgBox simple ' default property automagically mapped to toString()

simple.property1 = 9874
MsgBox simple.property1

simple.property2 = Now()
MsgBox simple.property2

simple.method1 ("A string parameter")


End Sub

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.

3. VB client accessing remote Java objects on UNIX, Linux, Windows and etc

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, re-register the JVM on the Windows VB client machine using the command regjvmcmd firstjvm[1350]:

Try running the VB client again.