PRB: ClassCastException Error When Using Excel Object in Java

Last reviewed: January 29, 1998
Article ID: Q169806
The information in this article applies to:
  • Microsoft Visual J++, versions 1.0, 1.1

SYMPTOMS

Attempting to create a new _ExcelApplicaton object and casting it to an Application interface, generates the following exception:

   exception:java.lang.ClassCastException:xl5en32/_ExcelApplication

RESOLUTION

To work around the problem call getDispatch on the Application object, in order to get at its COM interface. For the code mentioned in the MORE INFORMATION section, the workaround is to use a Workspace, and it's method .Application() to return an application object, and then use the getDispatch to get the Application interface:

     import xl5en32.*;
     import com.ms.com.*;
     public void TestExcel()
     {
   Variant v1 = new Variant();
        Application app;

        // Create A new worksheet
   Worksheet wks = (Worksheet)new _ExcelSheet();

   // Get hold of a dispatch on the application
   v1 = wks.Application();
   app = (Application)v1.getDispatch();
     }

MORE INFORMATION

Steps to Reproduce Behavior

  1. Run JAVATLB on Excel 5.0 Object Library. This creates Java class wrappers of the Excel objects and interfaces in the Type Library.

  2. Create a Java Application that contains the following code:

          import xl5en32.*;
    

          public void TestExcel()
          {
    
            _ExcelApplication x = new _ExcelApplication();
             Application xlApp = (Application)x;
          }
    
    

  3. Build the application and execute it.

  4. This causes a ClassCastException error when creating the Excel Automation object.

REFERENCES

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/visualj/
   http://support.microsoft.com/support/java/


Additional query words: excel exception java

Keywords : JCOM
Technology : kbInetDev
Version : 1.0 1.1
Platform : WINDOWS
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 29, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.