Accessing a Visual Basic EXE from Java |
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.
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.
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:
Click OK.
Click Open.
Click OK.
Click OK.
Click OK
Click OK
Click OK
Exit the class builder using File>Exit
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 |
Start to build the pure2vb.exe program using File>Make Pure2vb.exe (you will select the output directory in the next step):
When you exit the project, you may wish to save the project files in the same directory.
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.
Use com2java to generate Java proxies which can be used to access the COM Component you just created from any standard JVM:
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); } } } |
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:
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:
Now use a Web browser to peruse the generated documentation.
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.: