Using the Microsoft Java SDK to build COM objects in Java

or "I was only following orders..."

Here are some brief instructions on how to turn a Java object into a nicely-packaged COM object.

  1. Write a Java class.
  2. Here is mine
    public class JavaTest
    {
    	String s = null;
    
    	public void setString(String s)
    	{
    		this.s = s;
    	}
    
    	public String getString()
    	{
    		return this.s;
    	}
    
    	public static void main(String[] argv)
    	{
    
    		JavaTest jt = new JavaTest();
    		jt.setString("here is a string");
    		System.out.println(jt.getString());
    
    	}
    
    }
    
  3. Add the following notation to the head of the file
    /**
    @com.register ( clsid=9E469D52-EFE1-11d5-8A79-000103CFB75D,
    typelib=9E469D53-EFE1-11d5-8A79-000103CFB75D)
    */
    

    This is one of the causes of the rift between Sun and Microsoft. This is a precompiler directive that provides information needed to construct a COM object and its type library.
    The class now looks like this:
    /**
    @com.register ( clsid=9E469D52-EFE1-11d5-8A79-000103CFB75D,
    typelib=9E469D53-EFE1-11d5-8A79-000103CFB75D)
     
    */
    public class JavaTest
    {
    	String s = null;
    
    	public void setString(String s)
    	{
    		this.s = s;
    	}
    
    	public String getString()
    	{
    		return this.s;
    	}
    
    	public static void main(String[] argv)
    	{
    
    		JavaTest jt = new JavaTest();
    		jt.setString("here is a string");
    		System.out.println(jt.getString());
    
    	}
    
    }
    
  4. Use the GUIDGEN.EXE program to generate new values for clsid and typelib. This is important. These values are supposed to be unique, and will be used to create registry entries for your classes.
    The GUIDGEN.EXE user interface

    Note that I've selected "registry format". Each time you click the "New GUID" button, it will create a new GUID for you (duh!). When you click the "copy" button, the GUID will be copied to the clipboard. You can then paste it into your application. It'll look like this:
    {A8A128E1-F09D-11d5-8A7A-000103CFB75D}
    
    Delete the curly braces. I'm not sure if it's necessary, but I know that it works without them.
  5. rem compile the java classes with extensions enabled
    jvc /x- /nomessage *.java
    
  6. rem generate a type library from the .class file
    vjreg /typelib JavaTest.tlb JavaTest.class
    

    This step creates a type library from your class. This type library contains information that COM applications need in order to see your class, its methods, and their args.
  7. rem generate the OCX
    jexegen /reg /d /out:JavaTest.ocx /base:. JavaTest.tlb *.class
    

    This step creates an OCX. You could also say /out:JavaTest.dll if you like; there is no difference. The meaning of the flags is:
  8. Import the OCX into your COM development environment of choice.
    In Visual Basic, you'd do "Project->References". You should see "JavaTest" in the alphabetical list of available references. Check the checkbox, and you will be able to use the JavaTest class.
  9. Write a COM program to use your class. Here's a Visual Basic example:
    Private Sub Form_Load()
        Dim jt As Object
    	'create the object
        Set jt = CreateObject("JavaTest.JavaTest")
    	'invoke a method on the Java class
        jt.setString ("this is my string")
    	
        Dim s As String
    	'invoke another method
        s = jt.getString
    	'display the value returned from your Java class
        MsgBox s
    End Sub
    

Limitations

setenv.bat
make.bat
JavaTest.java
Form1.frm
Microsoft SDK for Java download site