com.sforce.soap.partner.LoginResult cannot be cast to 03 com.sforce.ws.bind.XMLizable   1 comment

http://www.tgerm.com/2010/08/wsc-apache-axis-classcastexception.html?utm_source=feedburner&utm_medium=twitter&utm_campaign=Feed%3A+TechnologyGerm+%28%3CTech%3E+Germ%29

This post is specially for those, having a Java Application(Production) working with SFDC web services via Apache-Axis stubs(WSDL2Java), but because of known advantages from WSC, you want to add some new functionality, like Bulk API as mentioned in SFDC blog here.

When we try to use both WSC and Apache Axis WSDL2Java stuff together in the same JVM or Project, we face Java Namespace(Package) clashes. You will certainly get Class-Cast errors like following on first run.

01 Exception in thread "main" java.lang.ClassCastException:
02 com.sforce.soap.partner.LoginResult cannot be cast to
03 com.sforce.ws.bind.XMLizable
04 at com.sforce.ws.bind.TypeMapper.readSingle(TypeMapper.java:618)
05 at com.sforce.ws.bind.TypeMapper.readObject(TypeMapper.java:495)
06 at
07 com.sforce.soap.partner.LoginResponse_element.loadFields(LoginResponse_element.java:68)
08 at
09 com.sforce.soap.partner.LoginResponse_element.load(LoginResponse_element.java:59)
10 at com.sforce.ws.bind.TypeMapper.readSingle(TypeMapper.java:619)
11 at com.sforce.ws.bind.TypeMapper.readObject(TypeMapper.java:495)
12 at com.sforce.ws.transport.SoapConnection.bind(SoapConnection.java:154)
13 at com.sforce.ws.transport.SoapConnection.receive(SoapConnection.java:128)
14 at com.sforce.ws.transport.SoapConnection.send(SoapConnection.java:103)
15 at com.sforce.soap.partner.PartnerConnection.login(PartnerConnection.java:791)
16 at
17 com.sforce.soap.partner.PartnerConnection.<init>(PartnerConnection.java:315)
18 at BulkExample.getRestConnection(BulkExample.java:171)
19 at BulkExample.runSample(BulkExample.java:43)
20 at BulkExample.main(BulkExample.java:34)

What creates this Namespace/Package hassle ?

When one has compiled the SFDC WSDLs using Apache Axis, Java classes get package names like  “com.sforce.soap.partner.*” for Partner WSDL. Similarly for WSC WSDL compilation we get the same package names for the WSDLs i.e. “com.sforce.soap.partner.*”. This happens because one has generated/compiled WSDLs using WSC with default options i.e.

1 java -classpath wsc.jar com.sforce.ws.tools.wsdlc wsdl jar.file

Nothing fails on compile time, but when you try running the code in parallel you get ClassCastException, I found one such issue posted on sfdc-wsc google code project.

Solution to this Package/Namespace conflict !

The solution to this issue is to specify your own prefix to Java packages when generating/compiling the WSDLs using WSC. So instead of using the default command above, just it to something similar below.

1 java -Dpackage-prefix=wsc -classpath wsc.jar com.sforce.ws.tools.wsdlc wsdl jar.file

Please note that I just added “-Dpackage-prefix=wsc” after the java command. Specifying this prefix will hint WSC WSDL2Java complier to generate all the Java classes with package name appended with “wsc”. So instead of java package “com.sforce.soap.partner.*”, new package name would be “com.sforce.soap.partner.wsc.*”. This will remove all namespace conflicts with Axis code in your project, now both Axis > LoginResult and WSC > LoginResult can both live happily together in the same JVM🙂

In the similar fashion, I would suggest generating rest of the stubs for other WSDLs like Enterprise, Apex and Metadata. So that there are no chances on java package namespace conflicts.

Why change Packaging structure for WSC only ?

I suggested to change packaging with WSC only, though you can do vice versa too i.e. change packaging for Apache Axis generated classes. This was done because, I am assuming following scenario

  • You are maintaining and enhancing a Java App using SFDC Web Services in production.
  • So you have already done WSDL2Java for your Apache Axis code, and your code has several dependencies on the generated Axis code.
  • You just want to add some new features like Bulk API on that application using SFDC WSC.
  • So rather breaking existing Axis code, its better to bring new WSC WSDL2Java stuff with corrected non-conflicting namespaces.

In case you are just starting with your java application, I suggest use only WSC. Its really fast and easy to use.

References

Posted 2011年04月4日 by gw8310 in salesforce

Tagged with ,

One response to “com.sforce.soap.partner.LoginResult cannot be cast to 03 com.sforce.ws.bind.XMLizable

Subscribe to comments with RSS.

  1. Thank you so much. I had this exact same problem and had no idea how to fix it. This saved me a lot of time, and was very well explained.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: