Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
12 replies Latest Post - ‏2014-01-17T20:09:25Z by PM6M_Charles_Ling
SystemAdmin
SystemAdmin
590 Posts
ACCEPTED ANSWER

Pinned topic Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

‏2013-02-18T17:49:04Z |
Note: I found my a resolution that worked for my environment. I struggled with this problem for awhile so I decided to post it here in case somebody else running into this problem.
I also had a question at the end of the post of whether or not it should be done the way I did.

Problem:
I had class cast exception with trying to instantiate a JAXBContext using
JAXBContext context = JAXBContext.newInstance( "my.jaxb.package");
The code is in an OSGI bundle that belongs WLP user feature I created. I didn't include/package any of the JAXB library in my OSGI bundle.

I found that the JAXBContextImpl can possibly comes from one of the following 3 jars :

1. JVM:
rt.jar (/com/sun/xml/internal/bind/v2/runtime/JAXBContextImpl)
2. WLP com.ibm.ws.xlxp.2.0.0_1.0.0.jar (com.ibm.xml.xlxp2.jaxb.JAXBContextImpl)
3. WLP com.ibm.ws.jaxb.tools.8.0.0_1.0.0.jar (/com/sun/xml/bind/v2/runtime/JAXBContextImpl)
Note: 2 & 3 are bundles that are available when jaxb-2.2 feature is enabled.

This problem didn't exist in WLP alpha version. I only see this problem when I moved up to the beta version.
This same code worked in the (WLP) alpha version howwever I had some class loading issue with some of the JVM classes. Moving up to the beta version got rid of the the JVM class loading issue but it instroduces the ClassCastException I described above.
Here is the exception:
java.lang.ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl incompatible with javax.xml.bind.JAXBContext
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:156)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:288)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:383)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:348)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:255)
at com.ibm.tscc.rtss.config.impl.JAXBPreferencesService.backupConfiguration(JAXBPreferencesService.java:1268)
at com.ibm.tscc.rtss.config.impl.JAXBPreferencesService.loadConfiguration(JAXBPreferencesService.java:213)
at com.ibm.tscc.rtss.config.impl.JAXBPreferencesService.<init>(JAXBPreferencesService.java:130)
at com.ibm.tscc.rtss.config.PreferencesServiceFactory.getPreferencesService(PreferencesServiceFactory.java:69)
at com.ibm.tscc.rtss.prefs.spif.PreferencesRuntimeServiceImpl.<init>(PreferencesRuntimeServiceImpl.java:75)
at com.ibm.tscc.rtss.prefs.spif.PreferencesSecurityServiceImpl.initialize(PreferencesSecurityServiceImpl.java:91)
at com.ibm.tscc.rtss.spif.SecurityServiceFactory.startupServices(SecurityServiceFactory.java:88)
at com.ibm.tscc.rtss.spif.SecurityServiceFactory.initialize(SecurityServiceFactory.java:166)
at com.ibm.tscc.pep.common.RTSSEmbedded.initialize(RTSSEmbedded.java:71)
at com.ibm.tscc.rtss.agent.startup.servlet.AgentStartupServlet.init(AgentStartupServlet.java:56)
at javax.servlet.GenericServlet.init(GenericServlet.java:161)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.init(ServletWrapper.java:320)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.loadOnStartupCheck(ServletWrapper.java:1356)
at com.ibm.ws.webcontainer.webapp.WebApp.doLoadOnStartupActions(WebApp.java:1025)
at com.ibm.ws.webcontainer.webapp.WebApp.commonInitializationFinally(WebApp.java:993)
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:905)
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:5940)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApp(DynamicVirtualHost.java:421)
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.createRunnableHandler(DynamicVirtualHost.java:241)
at com.ibm.ws.http.internal.VirtualHostImpl.discriminate(VirtualHostImpl.java:181)
at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.ready(HttpDispatcherLink.java:169)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:448)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleNewRequest(HttpInboundLink.java:382)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.processRequest(HttpInboundLink.java:282)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.ready(HttpInboundLink.java:253)
at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:174)
at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:83)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.requestComplete(WorkQueueManager.java:502)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.attemptIO(WorkQueueManager.java:550)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager.workerRun(WorkQueueManager.java:899)
at com.ibm.ws.tcpchannel.internal.WorkQueueManager$Worker.run(WorkQueueManager.java:981)
at com.ibm.ws.threading.internal.Worker.executeWork(Worker.java:439)
at com.ibm.ws.threading.internal.Worker.run(Worker.java:421)
at java.lang.Thread.run(Thread.java:736)
Here is how I resolve it:
Change from
JAXBContext context = JAXBContext.newInstance( "my.jaxb.package");
to
JAXBContext context = JAXBContext.newInstance( "my.jaxb.package", this.getClass().getClassLoader());

Question:
As mentioned earlier, I had some JVM class loading issue in alpha version that seems to be fixed on the beta version. Considering that this problem didn't exist in the alpha version, is it a side effect from the class loading fix in WLP?
Is the exception above an intended behavior for JAXBContext context = JAXBContext.newInstance( "my.jaxb.package") and that I should always call the newInstance() with a classloader as I did in the change above?
  • JohnYiXiao
    JohnYiXiao
    1 Post
    ACCEPTED ANSWER

    Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

    ‏2013-02-20T02:36:34Z  in response to SystemAdmin
    Hi,
    The root cause is using the jdk's jaxb api but jaxb-2.2 feature's implementation.

    When using JAXBContext context = JAXBContext.newInstance( "my.jaxb.package");
    will use the thread current context loader(TCCL) to load the jaxbcontext impl, if you enable the jaxb-2.2 feature, the com.ibm.xml.xlxp2.jaxb.JAXBContextImpl will be found.

    Using JAXBContext context = JAXBContext.newInstance( "my.jaxb.package", this.getClass().getClassLoader());
    will use the bundle class loader( as you mentioned "The code is in an OSGI bundle that belongs WLP user feature I created"), the bundle class loader will find the JAXBContextImpl in jdk, so it won't cause any ClassCastException.

    My suggestion is:
    1 Do not enable the jaxb-2.2 feature, then there is only one edition of JAXBContextImpl in server.
    2 If you must enable jaxb-2.2 feature, specify the import jaxb package version to "2.2" in your bundle.

    Hope it can help you.
    • SystemAdmin
      SystemAdmin
      590 Posts
      ACCEPTED ANSWER

      Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

      ‏2013-03-07T15:47:27Z  in response to JohnYiXiao
      Thank you for your response.
      I enabled jaxws feature which automatically enable jaxb-2.2 feature so I can't do suggestion #1.
      I will try with suggestion #2.

      Thanks again,
      An
    • husseinzx
      husseinzx
      5 Posts
      ACCEPTED ANSWER

      Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

      ‏2013-07-11T14:56:48Z  in response to JohnYiXiao

      Hello john,

       

      For solution # 1:

      I tried the below but still getting an exception:

      JAXBRIContext jaxbContext = (JAXBRIContext) JAXBContext.newInstance("javax.xml.bind",Test.class.getClassLoader());
       jaxbContext.createBinder(com.ObjectFactory.class);

       

          <cause type="java.lang.Throwable" description="cause"></cause>
          <errorCode type="java.lang.String" description="errorCode"></errorCode>
          <linkedException type="java.lang.Throwable" description="linkedException"></linkedException>
          <localizedMessage type="java.lang.String" description="String">"javax.xml.bind" doesnt contain ObjectFactory.class or jaxb.index</localizedMessage>
          <message type="java.lang.String" description="String">"javax.xml.bind" doesnt contain ObjectFactory.class or jaxb.index</message>
          <stackTrace type="[Ljava.lang.StackTraceElement;" description="StackTraceElement;">
            <element type="java.lang.StackTraceElement" description="StackTraceElement">com.ibm.jtc.jax.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:231)</element>

       

      Could you please tell me how can I disable  jaxb-2.2 feature.

       

      • Ivan_Xu
        Ivan_Xu
        8 Posts
        ACCEPTED ANSWER

        Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

        ‏2013-07-12T02:49:39Z  in response to husseinzx

        Hi,

        It looks like the original question was raised in Feb, not sure which wlp version you are using. Could you try the 8.5.5 GA version, it should be fixed after changing the SPI looking up mechanism for JAXB.

        For the errors in your latest post, you may not pass javax.xml.bind while creating the JAXBContext, the paramter could be your JAXB classes, your package name, ObjectFactory.class and etc. Use your original codes should be ok.

        JAXBContext context = JAXBContext.newInstance( "my.jaxb.package");

        Thanks.

        • husseinzx
          husseinzx
          5 Posts
          ACCEPTED ANSWER

          Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

          ‏2013-07-12T07:20:28Z  in response to Ivan_Xu
          Hello Ivan,

          Thanks for the reply. We are using BPM v8.0.1.1. I tried the below snipped but I am still facing the same issue.

          Note that I am using the client as part of a java integration service.

           

           JAXBRIContext jaxbContext = (JAXBRIContext) JAXBContext.newInstance("com.report", this.getClass().getClassLoader());

            JAXBRIContext jaxbContext = (JAXBRIContext) JAXBContext.newInstance("com.report",);

           

           
          • Ivan_Xu
            Ivan_Xu
            8 Posts
            ACCEPTED ANSWER

            Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

            ‏2013-07-12T07:43:32Z  in response to husseinzx

            If you still got the same exception in your first email, I am thinking that BPM v8.0.1.1 did not use the Liberty v8.5.5  GA version.

            As you mentioned,  if the class com.ibm.tscc.rtss.config.impl.JAXBPreferencesService is included in a bundle from your user feature, you may need to check whether the target bundle import jaxb api with version=2.2.

            Also, from your last email, it looks like that you are using an internal class JAXBRIContet from JAXB RI. If you would really like to do that, you may need to disable the IBM JAXB implementation, or you may still get CCE while converting com.ibm.xml.xlxp2.jaxb.JAXBContextImpl to JAXBRIContext. One solution for this is to set the property -Dcom.ibm.xml.xlxp.jaxb.opti.level=0, which will disable IBM JAXB implementation (Note this will bring performance downgrading for JaxWs).

            BTW, in the 8.5.5 GA version, you will not be able to import JAXBRIContext class in your bundle, as it is only for interal use, not publish as third-party API.

            Thanks.

            • husseinzx
              husseinzx
              5 Posts
              ACCEPTED ANSWER

              Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

              ‏2013-07-12T07:51:44Z  in response to Ivan_Xu

              Hello Ivan,

              I am using jaxws in order to build a webservice client. Is there a way to change my client to use com.ibm.xml.xlxp2.jaxb.JAXBContextImpl?

              Thanks,

              • Ivan_Xu
                Ivan_Xu
                8 Posts
                ACCEPTED ANSWER

                Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

                ‏2013-07-12T08:01:53Z  in response to husseinzx

                Hmm...

                I am just revisting the usage for IBM JAXB, it is possible that, even configured the system property, the return JAXBContextImpl is still from xlxp package, which means that you still could not cast it to JAXBRIContext. Could you double confirm this ?

                Aslo, does this solve the original issue in your first email  after importing 2.2 version API ?

                Thanks.

                • husseinzx
                  husseinzx
                  5 Posts
                  ACCEPTED ANSWER

                  Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

                  ‏2013-07-12T08:17:13Z  in response to Ivan_Xu

                  Hello Ivan,

                  Where I can set the system property in the BMP server? I am not sure if it is the right place, I added in 

                  Application servers > server1 > Custom properties the property you provide but the issue still the same.

                  I reproduced the issue in my IDE by adding jaxws-api-2.2.1.jar to my classpath.I was able to solve it by adding "-Djavax.xml.bind.JAXBContext=com.sun.xml.internal.bind.v2.ContextFactory" iwhen running it in my IDE.

                  • Ivan_Xu
                    Ivan_Xu
                    8 Posts
                    ACCEPTED ANSWER

                    Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

                    ‏2013-07-12T08:46:26Z  in response to husseinzx

                    Oh, I am a little confused with your answers. Are you using WebSphere full profile ? From your stacktrace and discription, all those are for Liberty profile.  All of my suggestions are for the issues in your first email.

                    I would really suggest to re-clarify the issue you encourntered,  that will be helpful to know the real scenario ;-)

                    In your first email, the CCE occurred in Liberty beta, then later you said that you are using BPM server (should be run on WebSphere full profile), and fixing the issue in IDE ?

                    Thanks.

                    • husseinzx
                      husseinzx
                      5 Posts
                      ACCEPTED ANSWER

                      Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

                      ‏2013-07-12T08:57:29Z  in response to Ivan_Xu

                      Hello Ivan,

                      We are using BPM server v8.0.1.1. In the process designerI we are  trying to build a java integration service which use an external lib.

                      My external lib use the jaxws *web service client" in order to invoke an external webservice.

                      The external lib is developed in any java IDE ( we are using eclipse)

                       

                      Thanks,

                       

                      • PM6M_Charles_Ling
                        PM6M_Charles_Ling
                        1 Post
                        ACCEPTED ANSWER

                        Re: Issue with JAXB:ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl

                        ‏2014-01-17T20:09:25Z  in response to husseinzx

                        Hello,

                        I'm encountering a similar issue when unmarshalling XML data from an IMS system.

                        [err] Caused by: 
                        [err] javax.xml.bind.JAXBException: Unable to create context
                         - with linked exception:
                        [java.lang.ClassCastException: com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl incompatible with javax.xml.bind.JAXBContext]
                        [err] at javax.xml.bind.ContextFinder.find(ContextFinder.java:70)
                        [err] at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:77)
                        [err] at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:73)
                        [err] at com.ibm.ims.dli.PSBImpl.unmarshalPSB(PSBImpl.java:4630)
                        [err] at com.ibm.ims.dli.PSBImpl.convertDBView(PSBImpl.java:903)
                        [err] ... 79 more
                        [err] Caused by: 
                        [err] java.lang.ClassCastException: com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl incompatible with javax.xml.bind.JAXBContext
                        [err] at javax.xml.bind.ContextFinder.find(ContextFinder.java:66)
                        [err] ... 83 more

                         

                        Here's my WASLP info:

                        Product name: WebSphere Application Server
                        Product version: 8.5.5.1
                        Product edition: zOS

                         

                        I have the following, among others, features enabled:

                        jaxb-2.2 [1.0.0]
                        jaxrs-1.1 [1.0.0]
                        jaxws-2.2 [1.0.0]

                         

                        There are two places containing jaxb code:

                        • com.ibm.ws.javaee.jaxb2.2_1.0.1.jar, which is bundled with WAS
                        • rt.jar, which is bundled with the JRE

                         

                        I've tried the following:

                        1. Disabling jaxb-2.2
                        2. Setting javax.xml.bind.JAXBContext=com.sun.xml.internal.bind.v2.ContextFactory
                        3. Placing jaxb-api.jar inside /lib/endorsed
                        4. Setting class loader delegation to "parent first"

                        I'm still seeing the same error. I appreciate any help you can provide.

                        Thanks,

                        Charles