=================================================== Frequently Asked Questions regarding Java/COM/DCOM =================================================== PREFACE: This document is a compilation of frequently asked questions and their answers about Java/COM/DCOM which have been gathered from the microsoft.public.visualj/com-support newsgroups. Although efforts have been made to find obvious errors, there is no guarantee that the information in this document is error-free. The FAQ maintainer, or anyone else associated with this document, assumes NO liability for the content or use of this document. Please post a new thread to the appropriate microsoft.public.java.visualj++ forum if you have follow-up questions on these issues. If you have a bug report to submit, please do so at http://www.microsoft.com/visualjSupport. If you have a question that is not in the FAQ, please post it. We will be posting FAQ's regularly to this forum. We monitor the forum traffic. If you have an announcement of value to other users, please check first to see if anyone else made the announcement already, then post it. Use common sense. This kind of discussion adds to the value of the newsgroup. While much of this material comes directly from newsgroup discussions, we also rely on your submissions to improve the quality and inclusiveness of this document. If you have found a hint, tip, trick, work-around, etc., please write it up and send it to us! Direct any comments/suggestions to: radhikar@microsoft.com ----------------------- 1. I am trying to use an ActiveX automation object written in Java (the COMCallingJava sample) from Visual Basic but I keep getting "Out of Memory" errors. This is normally caused by the Operating System not being able to find the Java class file that implements the ActiveX automation server. The class file that implements the Server must be copied to the java classpath (for instance: \windows\java\lib) for OLE object creation to work properly for Java class files. A common example of this is the COMCallingJava sample application supplied with VJ++. It will work fine when the sample is first built because the CEuclid.class is in the local directory (which the Developer Studio adds to the classpath before running Internet Explorer). Trying to run the VBDriver Visual Basic sample code supplied in the VBDriver project subdirectory will then fail with the "Out of Memory" error. This can be remedied by copying CEuclid.class to the \windows\java\lib directory. You can also set the output directory in the project build options so that the class file is automatically built in the appropriate classpath directory. This information is also documented in the following Knowledge Base article; Q163720: Getting Out Of Memory Error with COMCallingJava. 2. Why does Java misinterpret a Boolean value that is returned by a COM object? This is a bug, which has been fixed with the latest Microsoft Virtual Machine for Java. Please follow the link to http://www.microsoft.com/java to download the latest Microsoft Virtual Machine for Java. This version of the VM for Java requires Internet Explorer 3.01 or later. This information is also documented in the Microsoft KnowledgeBase article; Q162966: COM Object Returns Incorrect Value for Bool. 3. Why do I get a "java.lang.ClassFormatError", when I call Automation methods from VJ++ that takes parameters of Pointer type BSTR*, long*? This problem specifically occurs when using Automation Server methods created with the MFC Class Wizard. This can be fixed by modifying the .ODL file with [in,out] attributes on the automation methods that takes pointer type arguments: methods: // NOTE - ClassWizard will maintain method information here. // Use extreme caution when editing this section. //{{AFX_ODL_METHOD(PtrTest) [id(1)] void TestBstr([in,out]BSTR* arg1); [id(2)] void TestLong([in,out]long* arg1); //}}AFX_ODL_METHOD This allows JavaTlb to generate a valid class file, and the server works as expected when called from Java or Visual Basic. This information is also documented in the following Knowledge Base article; Q165550 : BUG: JavaTLB Generates Invalid Classes for MFC Server. 4. Why do I get an "exception:java.lang.ClassCastException:xl5en32/_ExcelApplication" error, when creating a new _ExcelApplication class and type-casting it to the Application interface? Inorder to get to the Application interface you need to call getDispatch on the Application object. The following code snippet illustrates how to make an Excel Application visible from Java and also shows how to get to the Application interface: This code snippet uses the Excel 5.0 Object Library: import xl5en32.*; import com.ms.com.Variant; public void TestExcel() { Application app; Variant vTemp = new Variant(); // Create A new worksheet Worksheet wks = (Worksheet)new _ExcelSheet(); // Get hold of a dispatch on the application vTemp = wks.Application(); app = (Application)vTemp.getDispatch(); // Make Excel Visible vTemp.putInt(Constants.xlVisible); app.putVisible(vTemp); } This information is also documented in the following Knowledge Base article; Q169806 : PRB: ClassCastException Error When Using Excel Object in Java. 5. Does the Microsoft VM for Java support Active X Control Containment? Currently, the Microsoft VM for Java build 1518 does not support the containment of ActiveX controls. You can't place an ActiveX control that has a user interface or that sources or sinks events directly onto an applet without implementing all of the ActiveX control containment and eventing interfaces yourself. To be clear, you can use ActiveX automation servers from Java and you can write ActiveX automation servers in Java utilizing the Microsoft Java Virtual Machine's ability to expose Java classes as ActiveX objects. You can also have ActiveX controls and Java applets on the same Web page and have them interact (see the OLEControls sample in Visual J++). However the SDK for Java 2.0 Beta 2 provides ActiveX Control Containment support through Beans. It exposes any Bean as an ActiveX Control and any ActiveX Control as a Bean. The SDK also includes samples that illustrates how to do this. You can download this SDK from http://www.microsoft.com/java. 6. How do I use the COMCallingJava sample as a DCOM Server? First you should build the sample and run it as a conventional (InProcess) automation server. Do this by following the instructions outlined in the readme.html that is installed with the sample. More information on how to use this sample via DCOM, can be found in the KnowledgeBase article; Q162164: Using Java Servers and DCOM. 7. When I try to use JavaReg with the /surrogate switch inorder to use my Automation Server written in Java as a DCOM server, the java console pops up and closes when run on a remote machine and my client application hangs? There are various problems that you may experience when you try to use an Automation Server written in Java as a DCOM server. These can include hanging, timeouts, out of memory errors, interface not found errors, and other errors. These errors are generally caused by not registering the Java COM object properly on both the client and DCOM server machines. The JAVAREG(version 1.0) that shipped with Microsoft Visual J++, versions 1.0 and 1.1 implemented a surrogate process itself, but it had some bugs making it difficult to setup Java DCOM servers and clients. This bug is documented in the KnowledgeBase article Q162164: BUG: Using Java Servers and DCOM. The DCOM support now in Windows'95 and Windows NT 4.0 with SP2 and SP3 supports a surrogate as part of the system, so the surrogate support with JAVAREG 1.0 has been dropped. JAVAREG 2.0 is a new tool that ships with SDK for Java 2.0 Beta 2 and this tool has fixed the bugs associated with JAVAREG 1.0 and supports remote access to a Java COM object using the system provided surrogate process. This information is also documented in the following Knowledge Base article; Q173790 : HOWTO: Using DllSurrogate support for Java/DCOM servers. You can download this SDK from http://www.microsoft.com/java. 8. Why do I get "Failed to Create Object" error message, when trying to instantiate a COM object written in Java? The following is a checklist that provides some of the common causes for this error. Make sure: 1. The java class is in the classpath. 2. Your java method is declared 'public'. 3. You need to register the class file (using javareg.exe). 4. Install the latest Java VM from http://www.microsoft.com/java if you are attempting to use CreateObject(). 5. Follow the steps in the Visual J++ sample named COMCALLINGJAVA. This information is also documented in the following Knowledge Base article; Q167953: Possible Reasons for "Failed to create object". 9. Why don't I need to use IDL anymore when creating a Java com object that is used through IDispatch? This is due to the new auto IDispatch feature that has been added to the latest Mirosoft Java VM. JavaTLB was originally created to generate get/put accessor methods for propget/propput methods declared in the Type Library. This Auto IDispatch feature lets all public methods get automagically exposed via IDispatch and public data members exposed as properties - you can even utilize this with javac.exe compiled classes if you use javareg to register them (or manually create the proper reg entries). Therefore you don't need to use an IDL/ODL file if you are just using java com objects through IDispatch - the auto IDispatch mechanism comes in handy. This information is also documented in the following Knowledge Base article; Q172202: INFO: Implementing Java Automation Objects using AutoIDispatch. 10. Is there a way to disable this Auto IDispatch mechanism of the VM? You can implement the system interface com.ms.com.NoAutoScripting to disable the Auto IDispatch for all instances of a class. 11. I get ComFailException:<80020005h>Type Mismatch error, when trying to use the Navigate method of the IWebBrowserApp interface? How do I invoke Internet Explorer from Java and navigate to a URL? The following code snippet illustrates creating a Java Object based on the Microsoft Internet Explorer and calling the Navigate method: 1. Create a default Java Applet or an Application. 2. Run JavaTLB on Microsoft Internet Controls. import com.ms.com.*; import shdocvw.*; public class Navigate { IWebBrowserApp testBrowser = null; public static void main( String args[] ) { IWebBrowserApp testBrowser = (IWebBrowserApp)new InternetExplorer(); testBrowser.getApplication(); testBrowser.putVisible(true); Dispatch.call(testBrowser, "Navigate", "http://www.microsoft.com"); } } The Dispatch.call method is documented in the Microsoft SDK for Java Documentation, which can be found at http://www.microsoft.com/java/sdk/x/exten025.htm. 12. I am able to automate Excel7.0 from Java, but I am finding the classes and interfaces to be quite different in Excel8. How do I automate Excel8.0 from Java? The following code snippet illustrates how to automate Excel8.0 from Java. It makes an Excel8.0 application visible and also open an existing .xls file with data in it. This code snippet uses the Excel 8.0 Object Library: import excel8.*; import com.ms.com.*; _Global globXL=null; _Application appXL=null; Workbooks books=null; _Workbook book = null; try{ globXL = (_Global)new Global(); appXL = (_Application)globXL.getApplication(); appXL.putVisible(0,true); books = (Workbooks)appXL.getWorkbooks(); Variant vTemp = new Variant(); vTemp.putString("c:\\book1.xls"); Variant vOptional = new Variant(); vOptional.noParam(); book = (_Workbook)books.Open("c:\\book1.xls",vOptional,vOptional, vOptional,vOptional,vOptional,vOptional, vOptional,vOptional,vOptional,vOptional, vOptional,vOptional,0); } catch(ComFailException e) { System.out.println(e.getMessage()); } This information is also documented in the following Knowledge Base article; Q169796: HOWTO: Automate Excel from Java. 13. When I use JavaTLB to generate classes from some COM object libraries, I get an error J5008. Should I be concerned? Often, the cause of this warning is that the method uses a type that is not supported in Java. All methods that produce a J5008 warning are omitted from the class file. Note that JavaTLB has been replaced by JACTIVEX in the SDK for Java 2.0 Beta2 release. 14. I get a 'java.lang.NoClassDefFoundError: com/ms/com/LicenseMgr' error when I run an applet generated by the Database Wizard for Java? When an applet is run in Internet Explorer and outside of Developer Studio, the applet is not trusted and does not have access to ILicenseMgr. This problem is by design since only trusted applets should have access to the ILicenseMgr. The ILicenseMgr allows the creation of COM objects on the client. To fix the problem, create a CAB file that contains the classes of the applet and digitally sign the CAB with the CodeSign kit. For more information on Cab and Sign technology, look in the 'Cab&Sign' directory on the Visual J++ CD-ROM.For the latest info on CAB technology please refer to 'http://www.microsoft.com/workshop/prog/cab'. This information is also documented in the following Knowledge Base article; Q169810: PRB: java.lang.NoClassDefFoundError:com/ms/com/LicenseMgr Error. 15. Why does VB crash when calling a Java Object which has a method that returns a boolean value? This is a bug with the JavaTLB mapping VT_BOOL to a 4-byte value for vtable mappings. To workaround the problem change the return type of the Java function to "short" in the IDL/ODL files with -1 representing TRUE and 0 representing FALSE. This problem has been fixed for JACTIVEX, where it maps VT_BOOL to a 2-byte value. You can get JACTIVEX, by installing the SDK for Java 2.0 Beta 2 from http://www.microsoft.com/java. Note that JavaTLB has been replaced by JACTIVEX in the SDK for Java 2.0 Beta 2 release. 16. When I run a Java Application that calls a COM object like an ATL Server, I get a "NoClassDeffoundError" on the COM object. The error occurs on the line where I create a new COM object in the Java Class. One reason why you may get this error could be related to missing parameter attributes for the methods in the ATL servers IDL file. For Example if you have a method like the one below: [id(1), helpstring("method GetString")] HRESULT GetString(BSTR* mystr); JavaTLB executes fine on this TLB. However when you execute your Java Application that calls this COM object ie. ISomeInterface m_intf = (ISomeInterface) new CAtlObj, it fails at this line with a "java.lang.NoClassDeffoundError". To fix the problem, change the IDL method to include the [out] attribute: For Example the method looks like: [id(1), helpstring("method GetString")] HRESULT GetString([out]BSTR* strGUID); So check your methods in the IDL file so that they have the right attributes like [in],[out],[retval] and so on. Once modifying the IDL file, rebuild your ATL server, and rerun JavaTLB on the ATL Servers Type Library so that the changes gets reflected.