Jcom Reference |
![]() Previous |
![]() About Tools |
![]() Index |
![]() Next |
The jcom tool generates .java files for the classes and interfaces described by a type library. These .java files, once compiled into .class files, allow Java programs to use COM services.
To use a COM class from Java, you must first import it; this means creating a Java class that represents the COM class in the context of Java.
A type library is a mechanism defined by COM to store type information; each type library contains complete type information about one or more COM entities, including classes, interfaces, dispinterfaces, and others. For more information about type libraries, see the Win32 SDK. To find out how to use the interfaces available from a particular programmable control or automation server, consult the documentation provided by the vendor.
Note Jcom is similar to the javatlb tool (which it replaces), except jcom generates java source code (.java) rather than .class files. The generated Java source requires new functionality not present in older versions of the Visual J++ compiler. See Upgrading to Jcom from JavaTLB
The typical usage is as follows:
jcom filename
where filename is the name of the type library (.tlb, .olb, .ocx, .dll or .exe).
For example:
jcom widgets.tlb
This command first creates a \Widgets subdirectory underneath the trusted library directory specified by the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Java VM\TrustedLibsDirectory. If the trusted library directory is \Windows\Java\Trustlib, the preceding command creates the directory \Windows\Java\Trustlib\Widgets, and fills it with .java files—one for each class, structure, enumeration, and interface described by the widgets.tlb type library.
If the type library contains an importlib statement, jcom creates a separate Java package in a separate directory for the imported type library.
Note The packages and directories created by jcom always have all-lowercase names.
Jcom has been designed for backward compatibility with the JavaTLB tool. In most cases, upgrading to jcom should require only two modifications to your makefiles or build scripts:
The following table shows the correspondence between jcom and javatlb command-line options:
Javatlb | Jcom | Notes |
/d dir | /d dir | Option works identically |
/p package | /p package | Option works identically |
/p:b- | /p:b- | Option works identically |
/X:m- | /X:m- | Option works identically |
/U | N/A | Not supported in jcom* |
/U:T | N/A | Not supported in jcom* |
*There is no support for generating summary.txt files in jcom because jcom generates human-readable .java source. |
Jcom generates Java source files that use a new Microsoft extension known as the "@com directives." These directives must appear inside specially formatted comments, similar to the existing documentation comments syntax. A typical @com directive emitted by jcom might look like this:
/** @com.method(vtoffset=4, dispid=100, type=METHOD, name="Hello") @com.parameters() */ public void Hello();
The directives are significant and used to generate the extra information needed by the Java/COM integration subsystem in the Microsoft Virtual Machine for Java. Therefore, the compiler used to compile jcom's output must process these directives. If you are using the Microsoft Visual J++ compiler (jvc.exe), you must use version 1.02.3920 or later to compile jcom's output. Earlier versions of JVC will not issue an compile-time error but the class files they generate will not work.
This is a Beta release of the @com directive syntax and the syntax is subject to change before final release. Therefore, detailed documentation on the @com directive syntax is not being provided with this release.
There are two ways to implement automation objects in Java.
The following sections describe each of these methods.
The VM now implements IDispatch automatically for almost all Java objects (previously, it did so only for applets). This means that tools such as javatlb or jcom are no longer necessary simply to script a Java object from an automation controller such as Microsoft Visual Basic or Visual Basic Script. To implement a simple automation object in Java:
The second approach uses jcom to generate COM interfaces from typelibs. This approach requires more work but allows you to ensure that the exposed interface matches the typelib description exactly.
This approach requires creating an IDL (or ODL) describing your interface and a coclass which uses the [default] attribute to define the default interface. For example,
[ object, uuid(...), helpstring("IWidget"), pointer_default(unique) ] interface IWidget : IUnknown { HRESULT add([in] long x, [in] long y, [out,retval] long*pz); }; [ uuid(...), helpstring("CWidget") ] coclass CWidget { [default] interface IWidget; };
Invoke jcom on the typelib using the /cj switch. This causes jcom to emit an extra file named CWidgetImpl.java. The CWidgetImpl.java for this typelib looks like this:
public class CWidgetImpl implements IWidgetDefault,com.ms.com.NoAutoScripting { public int add(int x, int y) { throw new com.ms.com.ComFailException(0x80004001); //E_NOTIMPL } }
To create a Java implemention of CWidget, you can either extend the class:
public class MyWidget extends CWidgetImpl { public int add(int x, int y) { return x+y; } }
or use CWidgetImpl itself as a template for MyWidget (this is preferable as it eliminates a superclass).
The following section describes the command-line options that jcom can use.
Do not use this switch in conjunction with the /cj option.
Warning This feature is not supported by pre-4.0 versions of the VM and classes built with this option set will not load on versions prior to 4.0. If your class must run on versions prior to 4.0, you must not use this switch and callers of this method will need to catch ComSuccessException to avoid the exception.
IBeeper b = (IBeeper)new Beeper(); b.beep();
to be simplified to:
Beeper b = new Beeper(); b.beep();
In addition, this form speeds up method invocation. The tradeoff is that the coclass files are larger and any manual changes to the information generated by jcom must be made in both the coclass and the interface.
jcom /d c:\mypackages widgets.tlb
By default, the base directory is the value specified in the registry HKEY_LOCAL_MACHINE\Software\Microsoft\Java VM\TrustedLibsDirectory. The normal VM install sets this to %WINDIR%\java\trustlib.
If the /e option is not specified, jcom will recursively compile typelibs needed to resolve all references.
jcom /p gremlinsoft widgets.tlb
This places the .java files in the directory \windows\java\trustlib\gremlinsoft\widgets. This option allows you to define, for example, a company-specific directory tree that contains the .java files for all the COM objects produced by your company. The package parameter can have multiple levels; for example, the command
jcom /p gremlinsoft.toolworks widgets.tlb
would place the .class files in the directory \windows\java\trustlib\gremlinsoft\toolworks\widgets. To use these classes in your Java program, you would need to specify an import statement of the form
import gremlinsoft.toolworks.widgets.*;
in your source code.
jcom /p:b- /d c:\mypackages widgets.tlb
For example,
jcom -l myfiles.lst widget.tlb
creates a file called mylist.lst which can then be used as input to jvc:
JVC @myfiles.lst
If a relative pathname is given for the response file, it is placed in the current directory. Its location is not affected by the /d option.
[Custom] <typename>=<javatype>,<javamarshallertype> ...
Its purpose is to inform jcom that certain types are to be marshalled using a COM Custom Marshalling hook class.
© 1997 Microsoft Corporation. All rights reserved. Legal Notices.