Accessing Java objects as Visual Basic Collections

Visual Basic has a built-in COM class called Collection. J-Integra® provides support for accessing instances of java.util.Vector and java.util.List as VB Collections.

Create the Java Bridge

Just as was demonstrated in the VB-EJB examples, we will create a simple briding application that will return a java.util.LinkedList object that can be used in Visual Basic as Collection. 

The first step is to to Register the JVM. 

You will need to know the TCP/IP host name of the machine running the bridge you just started. If it is the same machine as the VB client, then use 'localhost', otherwise use the appropriate host name:
> D:\jintegra\bin\regjvmcmd.exe firstjvm localhost[7050]

Next create the Java Briging application:

This is the Main Java class. As you can see, it does nothing apart from register the JVM:

// This is the bridge. The VB client talks to this  
// process via DCOM, and this process talks to WebLogic via RMI. It all happens  
// behind the scenes in the J-Integra® runtime.  
 
import java.util.*; 
 
import com.linar.jintegra.*; 
 
public class Main { 
  public static void main(String[] args) throws Exception { 
    // Tell the J-Integra® Activator on the localhost that this JVM is alive, ready to receive  
    // remote requests, and it is called "firstjvm".  
    com.linar.jintegra.Jvm.register("firstjvm"); 
    Thread.sleep(6000000); // Sleep for an hour  
  } 
}

Cut and paste the above class from your Web browser into the Main.java file, build it using the javac Main.java.

Now run the bridge, setting the JINTEGRA_DCOM_PORT property to 7050

> java -DJINTEGRA_DCOM_PORT=7050 Main

Example Visual BASIC code

 
Private Sub Form_Load()
Dim c As Collection
Set c = GetObject("firstjvm:java.util.LinkedList")
c.Add "hello" ' List is now: "hello"
c.Add Now, , "hello" ' List is now: , "hello"
c.Add "Goodbye", , , "hello" ' List is now: now, "hello", "Goodbye"
c.Add "Before", , 3 ' List is now: now, "hello", "Before", "Goodbye"
c.Add "After", , , 4 ' List is now: now, "hello", "Before", "Goodbye", "After"
For Each e In c
MsgBox e
Next
c.Remove 2 ' List is now: now, "Before", "Goodbye", "After"
c.Remove "Goodbye" ' List is now: now, "Before", "After"
MsgBox c.Item(1)
MsgBox c.Count
End Sub

VB

Maps to java.util.Vector

Notes

Collection.Add(item)

aVector.addElement(item)

Collection.Add(item, , before)

aVector.insertElementAt(before - 1, item)

if before is a number

Collection.Add(item, , before)

aVector.insertElementAt(aVector.indexOf(before), item)

if before is not a number

Collection.Add(item, , , after)

aVector.insertElementAt(after, item)

if after is not a number

Collection.Add(item, , , after)

aVector.insertElementAt(aVector.indexOf(after) + 1, item)

if after is a number

Collection.Count

aVector.size()

Collection.Item(index)

aVector.elementAt(index - 1)

index must be a number

Collection.Remove(index)

aVector.removeElementAt(index - 1)

if index is a number

Collection.Remove(index)

aVector.removeElement(index)

if index is not a number

For Each x in aCollection

x will take on the value of each object in the aVector.elements() enumeration

VB

Maps to java.util.List

Notes

Collection.Add(item)

aList.target.add(item)

Collection.Add(item, , before)

aList.add(before - 1, item)

if before is a number

Collection.Add(item, , before)

aList.add(aVector.indexOf(before), item)

if before is not a number

Collection.Add(item, , , after)

aList.add(after, item)

if after is not a number

Collection.Add(item, , , after)

aList.add(aVector.indexOf(after) + 1, item)

if after is a number

Collection.Count

aList.size()

Collection.Item(index)

aList.get(index - 1)

index must be a number

Collection.Remove(index)

aList.remove(index - 1)

if index is a number

Collection.Remove(index)

aList.remove(index)

if index is not a number

For Each x in aCollection

x will take on the value of each object in the aList.listIterator() iterator