Accessing a Visual Basic EXE from Java

Accessing a Visual Basic EXE from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access a Visual Basic EXE from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Visual Basic. It provides bi-directional access of Java objects and COM components.

Contents

  1. Introduction
  2. Configure your environment
  3. Create the Visual Basic COM Component
  4. Run the Java Client on Local Windows Machine
    1. Generate Java Proxies
    2. Create the Example
    3. Compile and Run the example
    4. Optional step - automatically generate Java documentation
  5. Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

1 Introduction

You may read this example simply to get a feel for how easy it is to use J-Integra® to access COM components created from Microsoft Visual Basic. You can run the Java client on a Windows machine to access its local VB COM component, or run the Java client on a non-Windows machine (such as Linux) to access VB COM component installed on a remote Windows machine.

This example will require access to a machine that is running Windows, and has Microsoft Visual Basic installed on it. This example assumes you are using Microsoft Visual Studio 6.0.

2 Configure your environment

If you do wish to try out this example, you should first download and install the Java Developers Kit. You should also download and install the J-Integra® for COM.

We will be performing this example under D:\pure. Create that directory, and a vbexe and pure2vb directory under it. 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:

Accessing VB EXE from Java: Set environment variables

3 Create the Visual Basic COM Component

  1. Start up Visual Basic, and create a new project of type ActiveX EXE:

    Accessing VB EXE from Java: Create new project
  2. Edit the project properties, using Project>Project1 Properties:

    Accessing VB EXE from Java: Edit project properties
  3. Change the project name to pure2vb and add a description:

    Accessing VB EXE from Java: Change project name

    Click OK.

  4. Remove the default class that is created using Project>Remove Class1 (don't save the changes):

    Accessing VB EXE from Java: Remove default class
  5. Start adding a new class using Project>Add class module:

    Accessing VB EXE from Java: Add new class module
  6. Use the VB Class Builder tool:

    Accessing VB EXE from Java: Select the VB class builder tool

    Click Open.

  7. Create a new class in the builder, using File>New>Class:

    Accessing VB EXE from Java: Create new class
  8. Set the new class name to TestClass and set instancing to Global Multi Use:

    Accessing VB EXE from Java: Edit class properties
  9. Set a description for the class by going to the Attributes pane:
    Accessing VB EXE from Java: Edit class description

    Click OK.

  10. Create a new property using File>New>Property:

    Accessing VB EXE from Java: Create new property
  11. Set the property name to prop1 and set the data type to Variant:

    Accessing VB EXE from Java: Edit propery
  12. Select the Attributes pane, and enter a description for the property:

    Accessing VB EXE from Java: Edit property description

    Click OK.

  13. Create a new method using File>New>Method:

    Accessing VB EXE from Java: Create new method
  14. Set the method name to meth1, set the Return Data Type to Integer and click on the + button to add a parameter:

    Accessing VB EXE from Java: Edit method properties
  15. Set the parameter name to p1 and set its type to String:

    Accessing VB EXE from Java: Set parameter name

    Click OK

  16. Click on the + button to add a second parameter called p2, that is passed ByVal and is of type Object:

    Accessing VB EXE from Java: Add 2nd parameter

    Click OK

  17. The Method Builder should look like this:

    Accessing VB EXE from Java: Display method builder
  18. Select the Attributes pane, and enter a description for the method:

    Accessing VB EXE from Java: Edit method description

    Click OK

  19. Update the project from the Class Builder using File>Update Project:

    Accessing VB EXE from Java: Update project

    Exit the class builder using File>Exit

  20. Examine the generated code. Update the implementation of the meth1 method:
    	Public Function meth1(p1 As String, ByVal p2 As Object) As Integer
    	meth1 = Len(p1)
    	p1 = "What you think you said is '" & p1 & "'"
    	MsgBox "p2 is " + p2
    	p2.callBack "hello", 2345
    	End Function
    	

    If you are feeling adventurous, this is an alternative implementation that starts to give you a feel for the flexability of J-Integra®'s COM to Java bridging:
     

    	Public Function meth1(p1 As String, ByVal p2 As Object) As Integer
    	meth1 = Len(p1)
    	p1 = "What you think you said is '" & p1 & "'"
    	MsgBox "p2 is " + p2
    	p2.callBack "hello", 2345
    	Set p2Class = p2.getClass()
    	MsgBox "p2's class is " + p2Class
    	Set hashtableClass = p2Class.forName("java.util.Hashtable")
    	Set aHashtable = hashtableClass.newInstance
    	aHashtable.put "The key", 1234
    	MsgBox "The new Hashtable's toString method returns: " + aHashtable
    	End Function
    	
  21. Start to build the pure2vb.exe program using File>Make Pure2vb.exe (you will select the output directory in the next step):

    Accessing VB EXE from Java: Build the EXE

  22. Select the D:\pure\vbexe directory you created earlier:
    Accessing VB EXE from Java: Select the D:\pure\vbexe directory

    When you exit the project, you may wish to save the project files in the same directory.

4 Run the Java Client on Local Windows Machine

You can try this example on local Windows machine first to get a feel for how easy it is to use J-Integra® to access VB COM component from Java. Once you make it working on local machine, you can then try to run the Java client on a non-Windows machine to remotely access VB COM component on another Windows machine.

4.1 Generate the Java proxies

Use com2java to generate Java proxies which can be used to access the COM Component you just created from any standard JVM:

  1. From com2java, Select d:\pure\vbexe\pure2vb.exe as the type library to open (the type library is contained within the executable):
    Accessing VB EXE from Java: Select the type library
  2. Set the Java package to pure2vb
  3. Select D:\pure\pure2vb as the output directory
  4. Select Options from the Settings menu; and check the Options section of com2java tool to make sure that you selected correct options.
  5. Click on Generate Proxies
Accessing VB EXE from Java: Generate proxies

4.2 Create the example

  1. Create the file d:\pure\VbExample.java, by cutting and pasting from your Web Browser.

    Here is the file. The parts in bold are the most significant:
     

    import java.util.Date;
    import java.io.IOException;
    import java.net.UnknownHostException;
    import com.linar.jintegra.AutomationException;
    
    public class VbExample {
    
      public void callBack(String p1, int p2) {
        System.out.println("Callback has been called, with " + p1 + " and " + p2);
      }
    
      public String toString() { return "An instance of VbExample"; }
    
      public static void main(java.lang.String[] args) {
        try {
          // DCOM authentication: Make sure NT DOMAIN, Nt USER, Nt PASSWORD are valid credentials.
          // Uncomment this line if VcExample.java remotely accesses VB EXE:
          // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "USER", "PASSWORD");
    
          // Specify host name or IP address of VB EXE machine as parameter if
          // VcExample.java remotely accesses VB EXE.
          // pure2vb.TestClass testClass = new pure2vb.TestClass("123.456.789.0");
          pure2vb.TestClass testClass = new pure2vb.TestClass();
    
          // Try using the 'prop1' property (a VARIANT)
          testClass.setProp1("A String");
          System.out.println(testClass.getProp1());
    
          // Lets be a little more adventurous
          Object[] values = { "A String", new Date(), new Float(6543.43F), new Short((short)7654) };
          testClass.setProp1(values);
          values = null;  // Not *really* required
          values = (Object[])testClass.getProp1();
          for(int i = 0; i < values.length; i++) {
             System.out.println("values[" + i + "] (" + values[i].getClass() + ") = " + values[i]);
          }
    
          String[] p1 = { "A String" }; // Single elt array for 'ByRef' parameters
          int len = testClass.meth1(p1, new VbExample());
          System.out.println(p1[0]);
    
          // Release the objects -- Garbage collection will also automatically release them
          testClass.release();
        } catch(UnknownHostException uhe) {
          System.out.println("Cannot contact the host you specified");
        } catch(AutomationException ae) {
          System.out.println("The remote server returned an error: " + ae);
        } catch(IOException ioe) {
          System.out.println("Communications problem: " + ioe);
        }
      }
    }
  2. Compile the example using the javac VbExample.java command:

    Accessing VB EXE from Java: Compile the example

4.3 Compile and run the example

On the Java client machine, make sure your CLASSPATH and PATH environment variables are set up according to J-Integra® installation instructions. Compile and run the example in  J-Integra®'s native mode (you need to use DCOM mode if remotely accessing VB EXE):

javac VcExample.java
java -DJINTEGRA_NATIVE_MODE VbExample

Your COM Component will be automatically started up in the background, and the program will run:
Accessing VB EXE from Java: Display results

4.4 Optional step - automatically generate Java documentation

Remember all those descriptions you set when creating the class, property and method in Visual Basic?

Create a directory for the documentation: D:\pure\pure2vb_doc, and then use the javadoc tool to create the documentation using the command: javadoc -d pure2vb_doc pure2vb:

Accessing VB EXE from Java: Generate javadoc

Now use a Web browser to peruse the generated documentation.

5 Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

You can also run the Java client on a remote machine, such as Linux, Solaris, UNIX and AIX. For instance, if you run it on a Linux machine, then you must do the following.:

  1. Move the com2java tool to the Windows machine to generate the Java proxies from VB EXE, and then move the Java proxies from the Windows machine to the Linux machine.
  2. Install J-Integra® (the jintegra.jar file) on the Linux machine and include the jintegra.jar file and generated Java proxies in CLASSPATH.
  3. Use DCOMCNFG to configure VB EXE on the Windows machine.
  4. Pass correct login credentials to com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
    Refer to Configuring DCOM for Remote Access for more information about AuthInfo.setDefault.
  5. Pass the IP address or computer name of the VB EXE machine to the constructor of TestClass object in VbExample.java:
    pure2vb.TestClass testClass = new pure2vb.TestClass("123.456.789.0");
  6. Move VcExample.java to the Linux machine. Compile and run it in DCOM mode without using DJINTEGRA_NATIVE_MODE property:
    java VbExample