Example: Use an ActiveX client application to access a Java class or object
We can use Java proxy objects with the ActiveX to EJB bridge.
Use Java proxy objects with the ActiveX to EJB bridge, as follows:
- After an ActiveX client program (Visual Basic, VBScript, or Active Server Pages (ASP)) has initialized the XJB.JClassFactory object and thereby, the JVM, the client program can access Java classes and initialize Java objects. To complete this action, the client program uses the XJB.JClassFactory FindClass() and NewInstance() methods.
- In Java programming, two ways exist to access Java classes: direct invocation through the Java compiler and through the Java Reflection interface. Because the ActiveX to Java bridge needs no compilation and is a complete run-time interface to the Java code, the bridge depends on the latter Reflection interface to access its classes, objects, methods and fields. The XJB.JClassFactory FindClass() and NewInstance() methods behave very similarly to the Java Class.forName() and the Method.invoke() and Field.invoke() methods.
- XJB.JClassFactory.FindClass() takes the fully qualified class name as its only parameter and returns a Proxy Object (JClassProxy). We can use the returned Proxy object like a normal Java Class object and call static methods and access static fields. We can also create a Class Instance (or object), as described later in this section. For example, the following Visual Basic code extract returns a Proxy object for the java.lang.Integer Java class:
... Dim clsMyString as Object Set clsMyString = oXJB.FindClass("java.lang.Integer")
- After the proxy is created, we can access its static information directly. For example, we can use the following code extract to convert a decimal integer to its hexadecimal representation:
... Dim strHexValue as String strHexValue = clsMyString.toHexString(CLng(255))
- The equivalent Java syntax is: static String toHexString(int i). Because ints units in Java programming are really 32-bit (which translates to Long in Visual Basic), the CLng() function converts the value from the default int to a long. Also, even though the toHexString() function returns a java.lang.String, the code extract does not return an Object proxy. Instead, the returned java.lang.String is automatically converted to a native Visual Basic string.
To create an object from a class, you use the JClassFactory.NewInstance() method. This method creates an Object instance and takes whatever parameters the class constructor needs. Once the object is created, we have access to all of its public instance methods and fields. For example, we can use the following Visual Basic code extract to create an instance of the java.lang.Integer string:
... Dim oMyInteger as Object set oMyInteger = oXJB.NewInstance(CLng(255)) Dim strMyInteger as String strMyInteger = oMyInteger.toString
Related concepts
ActiveX to EJB Bridge
Example: ActiveX client application calling Java methods