EZ Jcom
Java to COM/ActiveX Bridge

The Basics of accessing Java using EZ JCom

This page covers some basic information that should be learned for using EZ JCom to access Java from languages such as C# and VB.

Adding the COM (ActiveX) DLL generated by EZ JCom to your project

EZ JCom will generate a COM DLL that provides access to the Java classes. This DLL needs to be added to your Visual Basic or C# (or other language) project. Typically, there is an "Add Reference" menu item for the project. In some cases, the "Add Reference" menu item has multiple tabs, in such cases, select the "COM" tab.

Once a reference has been added to the project, Intellisense will show the new library and its contents.


In Java, there can be multiple constructors for constructing an object. Since EZ JCom supports multiple languages via COM, the "new" operation in EZ JCom doesn't take parameters. Instead, when accessing Java via EZ JCom, one of the constructors must be called after instantiating a "new" object, and before accessing any methods or fields.

There are two exceptions -- for accessing "static" methods or fields of Java, a constructor does not have to be called. And for event objects in use for receiving Java events, a constructor does not have to be called.

Type Casting

How do I pass this object of class A that implements/extends class B, where the method I am calling only expects class B?

EZ JCom does not attempt to map the Java class and interface hierarchies in COM. Instead, Java casting is handled by a special type of constructor named ConstuctByCasting. When you use ConstructByCasting, you are sharing the underlying Java object, and are creating multiple COM objects of different types that refer to the same Java object.

This allows you to cast a C# or VB or other COM object that corresponds to a Java object into another type that corresponds to a Java superclass/subclass of the original Java object, or to a Java interface that's been implemented by the original object. By using this mechanism, all kinds of Java hierarchies can be handled in all COM supporting languages.

For instance, if method MyMethod expects a parameter of type java.security.Principal (a Java interface) and you have another type MySecurityPrincipal that implements java.security.Principal, the code for passing this parameter may look like this:

    // Construct the actual parameter
    MySecurityPrincipal msp = new MySecurityPrincipal();

    // Instantiate a "wrapper" and use ConstructByCasting
    // to share the underlying Java object as a different COM type.
    Principal p = new Principal();
    p.ConstructByPasting( msp );

    // Pass the wrapper as parameter.
    myObj.MyMethod( p );


The Java classes must be available in the classpath at runtime. There are a few different mechanisms available:
  • If you do not do anything, EZ JCom will use the CLASSPATH environment variable by default.
  • If you check the checkbox for "Dynamically set this classpath at runtime", EZ JCom will automatically set and use that classpath before loading Java.
  • You can programmatically set the classpath by setting the "JavaClasspath" property of the "Java" object. This must be done before Java gets loaded. (Java gets loaded at first use of any Java classes.)


Some other features that you should be aware of that EZ JCom provides:
  • Event handling from Java.
  • Displaying Java UI objects (Panel's or JPanel's etc) in COM-aware languages like C#, VB and VB.NET.
  • JVM Selection at runtime.
  • Exception deconstruction. You can add any custom Java exception classes to the list of Java classes to access, and retrieve the last Java exception in a thread as the "LastJavaException" property of the "Java" object.