BUG: ClassCastException Calling from Java to Java via COMID: Q202600
|
When you make a Java to Java call through COM using a user-defined or Visual J++ 6.0 generated class type, you might experience a ClassCastException when you do type casts.
This is a known limitation of the Java-COM support in current Microsoft Java Virtual Machines.
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
//JavaCOMClassA.java
/**
* @com.register ( clsid=39E0D51C-A704-11D1-9BAC-00A0C905438F, typelib=39E0D51A-A704-11D1-9BAC-00A0C905438F )
*/
public class JavaCOMClassA
{
public JavaCOMClassB GetB()
{
return new JavaCOMClassB();
}
}
Source File 2: JavaCOMClassB.java
//JavaCOMClassB.java
/**
* @com.register (clsid=39E0D51B-A704-11D1-9BAC-00A0C905438F,typelib=39E0D51A-A704-11D1-9BAC-00A0C905438F )
*/
public class JavaCOMClassB
{
private int m_iData = 5;
public void SetData(int iData)
{
m_iData = iData;
}
public int GetData()
{
return m_iData;
}
}
//Main.java
import com.ms.com.*;
import javacomclass.*;
public class Main
{
public static void main(String[] args)
{
int data;
try
{
JavaCOMClassA_Dispatch A = new JavaCOMClassA();
/**
* First, return a JavaCOMClassB user-defined COM object
* directly from JavaCOMClassA.GetB(). This should cause a
* ClassCastException when casting the Object to its appropriate
* disp interface.
*/
try {
Object obj = A.GetB();
JavaCOMClassB_Dispatch b = (JavaCOMClassB_Dispatch)obj;
}
catch(ClassCastException cce)
{
com.ms.wfc.util.Debug.println("ClassCastException calling JavaCOMClassA.GetB() directly");
}
/**
* This workaround uses the Object class returned from GetB() and
* the Static Dispatch class to invoke COM methods through
* IDispatch.
*/
Object B = A.GetB();
Variant V = Dispatch.call(B,"GetData");
data = V.getInt();
com.ms.wfc.util.Debug.println(new Integer(data).toString());
V = Dispatch.call(B,"SetData", new Integer(10));
V = Dispatch.call(B,"GetData");
data = V.getInt();
com.ms.wfc.util.Debug.println(new Integer(data).toString());
} catch(ClassCastException e) {
e.printStackTrace();
System.out.println(e.toString());
System.out.println(e.getMessage());
}
}
}
© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Stephen L. Horne, Microsoft Corporation
For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java,
please see the following pages on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/
http://support.microsoft.com/support/java/
Additional query words:
Keywords : kbCOMt kbJava kbJavaVM kbVJ600 kbGrpJava
Version : WINDOWS:6.0
Platform : WINDOWS
Issue type : kbbug
Last Reviewed: February 24, 1999