13 Replies Latest reply on Jul 15, 2017 10:17 AM by zdenek.hrib

    __redirected.__DocumentBuilderFactory ClassCastException

    rodrigorjo

      Hello everyone,

       

      I am migrating some web systems that use third party libraries. One of them use its own DocumentBuilderFactory class.

       

      I created a module for the libraries, and the classes in that module are being accessed fine by the applications.

       

      When I try to use DocumentBuilderFactory it gives a classCastException.

       

      I tryed to exclude DocumentBuilderFactory from javaee.api with both ways indicated at this post  https://community.jboss.org/message/637818#637818.

       

      Neither worked and I still keep recieving the error:

      java.lang.ClassCastException: __redirected.__DocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory

       

      Is it because __redirected.__DocumentBuilderFactory is not on javax.xml.parsers? so I guess the configuration to exclude <exclude path="javax/xml/parsers" /> would not work, right?

      If so, how can I resolve this issue? Where is __redirect.__DocumentBuilderFactory? Why is Jboss trying to use it instead of my own class.

       

      Thanks in advance and follows the 2 configurations I had tryed according to the answer in that post: (I also tryed many variations trying to exclude javax/xml/parsers from javaee.api)

       

      <?xml version="1.0" encoding="UTF-8"?>

      <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">

          <ear-subdeployments-isolated>true</ear-subdeployments-isolated>

          <deployment>

              <dependencies>

                  <module name="deployment.javaee.api" />

              </dependencies>

       

              <exclusions>

                  <module name="javaee.api" />

              </exclusions>

          </deployment>

          <module name="deployment.javaee.api">

              <dependencies>

                  <module name="javaee.api" export="true">

                      <imports>

                          <exclude path="javax/xml/parsers" />

                      </imports>

                  </module>

              </dependencies>

          </module>

      </jboss-deployment-structure>

       

      or

       

      <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">

        <deployment>

          <exclusions>

            <module name="javaee.api" />

          </exclusions>

          <dependencies>

              <module name="org.apache.commons.logging" />

          </dependencies>

        </deployment>

        <sub-deployment name="viabilidade.war">

          <exclusions>

            <module name="javaee.api" />

          </exclusions>   

          <dependencies>

              <module name="javaee.api" >

                  <imports>

                      <exclude-set>

                          <path name="javax/xml/parsers"/>

                      </exclude-set>

                  </imports>

              </module>

          </dependencies>

        </sub-deployment>

      </jboss-deployment-structure>

        • 1. Re: __redirected.__DocumentBuilderFactory ClassCastException
          rodrigorjo

          Where is this classs __redirected.__DocumentBuilderFactory, and how can I remove it from the classpath of my application?

          • 2. Re: __redirected.__DocumentBuilderFactory ClassCastException
            cfang

            This class is part of jboss as and may not be removed.

             

            Your error looks like a classloader mismatch.  Can you post the full stacktrace?  AS7 does some jaxp factory redirecting, e.g., DocumentBuilderFactory, but the DocumentBuilder should still be whatever you specified.

             

            How did you specify your jaxp factory settings?  via -D sysprops in server JVM?  I suppose so since you would need to override the JDK defaults.

             

            javax.xml.parsers are part of JDK, maybe falls under javax.api module in as7, why you reference javaee.api for that?

             

            I would focus on how to add the necessary dependency to your app, not removing/excluding certain things, under the assumption that anything else not used by your app should never interfere with your app in as7.

             

            Have you  tried packaged the custom xml library inside your app (EAR/lib/x.jar, or WEB-INF/lib/x.jar)? 

            • 3. Re: __redirected.__DocumentBuilderFactory ClassCastException
              rodrigorjo

              Right, it is javax.api, I don't know why I wrote javaee.api. Anyway, using javax.api instead of javaee.api I still get the same error. I also tried to put my jar in either, WEB-INF/lib and EAR/lib but without sucess. The classload mismatch continues.

               

              The jboss-deployement-structure.xml I am using is:

               

              <?xml version="1.0" encoding="UTF-8"?>

              <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">

                <deployment>

                  <exclusions>

                    <module name="javax.api" />

                  </exclusions>

                </deployment>

                <sub-deployment name="viabilidade.war">

                  <exclusions>

                    <module name="javax.api" />

                  </exclusions>  

                  <dependencies>

                      <module name="javax.api" >

                          <imports>

                              <exclude-set>

                                  <path name="javax/xml/parsers"/>

                              </exclude-set>

                          </imports>

                      </module>

                  </dependencies>

                </sub-deployment>

              </jboss-deployment-structure>

               

              and the full stack trace is:

               

              ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/viabilidade]] (MSC service thread 1-4) Exception starting filter JOSSOGenericServletFilter: javax.servlet.ServletException: Error starting SSO Agent : Unexpected exception parsing XML document from class path resource [josso-agent-config.xml]; nested exception is java.lang.ClassCastException: __redirected.__DocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory

                  at org.josso.servlet.agent.GenericServletSSOAgentFilter.startup(GenericServletSSOAgentFilter.java:103) [josso-servlet-agent-1.8.6.jar:1.8.6]

                  at org.josso.servlet.agent.GenericServletSSOAgentFilter.init(GenericServletSSOAgentFilter.java:118) [josso-servlet-agent-1.8.6.jar:1.8.6]

                  at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:447) [jbossweb-7.0.10.Final.jar:]

                  at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3269) [jbossweb-7.0.10.Final.jar:]

                  at org.apache.catalina.core.StandardContext.start(StandardContext.java:3865) [jbossweb-7.0.10.Final.jar:]

                  at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.0.Final.jar:7.1.0.Final]

                  at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)

                  at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)

                  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_20]

                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_20]

                  at java.lang.Thread.run(Thread.java:619) [rt.jar:1.6.0_20]

              Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [josso-agent-config.xml]; nested exception is java.lang.ClassCastException: __redirected.__DocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory

                  at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:420) [spring-beans-2.5.5.jar:2.5.5]

                  at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342) [spring-beans-2.5.5.jar:2.5.5]

                  at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) [spring-beans-2.5.5.jar:2.5.5]

                  at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) [spring-beans-2.5.5.jar:2.5.5]

                  at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) [spring-beans-2.5.5.jar:2.5.5]

                  at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149) [spring-beans-2.5.5.jar:2.5.5]

                  at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212) [spring-beans-2.5.5.jar:2.5.5]

                  at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113) [spring-context-2.5.5.jar:2.5.5]

                  at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.loadBeanDefinitions(ClassPathXmlApplicationContext.java:180) [xbean-spring-3.4.3.jar:3.4.3]

                  at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123) [spring-context-2.5.5.jar:2.5.5]

                  at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:423) [spring-context-2.5.5.jar:2.5.5]

                  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:353) [spring-context-2.5.5.jar:2.5.5]

                  at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:161) [xbean-spring-3.4.3.jar:3.4.3]

                  at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:51) [xbean-spring-3.4.3.jar:3.4.3]

                  at org.josso.agent.config.SpringComponentKeeperImpl.<init>(SpringComponentKeeperImpl.java:51) [josso-agent-1.8.6.jar:1.8.6]

                  at org.josso.agent.config.SpringComponentKeeperFactoryImpl.newComponentKeeper(SpringComponentKeeperFactoryImpl.java:33) [josso-agent-1.8.6.jar:1.8.6]

                  at org.josso.agent.Lookup.getComponentKeeper(Lookup.java:125) [josso-agent-1.8.6.jar:1.8.6]

                  at org.josso.agent.Lookup.lookupSSOAgent(Lookup.java:102) [josso-agent-1.8.6.jar:1.8.6]

                  at org.josso.servlet.agent.GenericServletSSOAgentFilter.startup(GenericServletSSOAgentFilter.java:93) [josso-servlet-agent-1.8.6.jar:1.8.6]

                  ... 10 more

              • 4. Re: __redirected.__DocumentBuilderFactory ClassCastException
                cfang

                Can you post a test app to reproduce it?  You could also debug the springframework code to check the actual classloader for DocumentBuilderFactory and __redirected.__DocumentBuilderFactory.

                 

                Normally DocumentBuilderFactory should be loaded by java bootstrap loader, and __redirected.__DocumentBuilderFactory by as system loader, and so are assignable.

                • 5. Re: __redirected.__DocumentBuilderFactory ClassCastException
                  nicolyra

                  I've got the same problem. Did you resolve it?

                  Can you expose your solution?

                  • 6. Re: __redirected.__DocumentBuilderFactory ClassCastException
                    sun99

                    Has any one solved this issue, i am still getting following exception

                     

                    JBWEB000289: Servlet InitializationServlet threw load() exception: java.lang.ClassCastException: __redirected.__DocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory

                        at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source) [DocumentBuilderFactory.class:]

                        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:849) [DOMConfigurator.class:]

                        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755) [DOMConfigurator.class:]

                        at org.apache.log4j.xml.XMLWatchdog.doOnChange(DOMConfigurator.java:1120) [XMLWatchdog.class:]

                        at org.apache.log4j.helpers.FileWatchdog.checkAndConfigure(FileWatchdog.java:89) [FileWatchdog.class:]

                        at org.apache.log4j.helpers.FileWatchdog.<init>(FileWatchdog.java:58) [FileWatchdog.class:]

                        at org.apache.log4j.xml.XMLWatchdog.<init>(DOMConfigurator.java:1112) [XMLWatchdog.class:]

                        at org.apache.log4j.xml.DOMConfigurator.configureAndWatch(DOMConfigurator.java:735) [DOMConfigurator.class:]

                        at org.apache.log4j.xml.DOMConfigurator.configureAndWatch(DOMConfigurator.java:718) [DOMConfigurator.class:]

                     

                     

                    Background:

                     

                    I have deployed osgi bundle on jboss-eap-6.1.0, before that i have started jboss server as follows

                     

                    #standalone.bat -c standalone-osgi.xml

                     

                    I have created osgi bundle from "war"  by updating MANIFEST.MF with osgi headers.

                    I have no issue with deployment.

                     

                    My OSGi bundle has xml-apis.jar in WEB-INF/lib which i have added as part of Bundle-ClassPath:

                     

                    any help is appreciated

                    • 7. Re: __redirected.__DocumentBuilderFactory ClassCastException
                      sun99

                      Got resolved by removing xml-apis and xercesImpl from Bundle-ClassPath:

                      Looks like these jars are conflicting with JBOSS modules .

                       

                      I don't understand why if jars in war don't conflict with JBoss ,

                      but in OSGi  (in WEB-INF\lib) they conflict with JBoss jars

                      • 8. Re: __redirected.__DocumentBuilderFactory ClassCastException
                        trybik03

                        For others that may be coping with similar problem:

                        I was migrating a large app from JBoss 5.1 to WildFly 8. I got the same error as in the thread's title.

                        Here is what happened:

                        • DocumentBuilderFactory is present in JDK, but it always been
                        • WildFly declares its own factory. In start up logs you can see
                          javax.xml.parsers.DocumentBuilderFactory = __redirected.__DocumentBuilderFactory
                          The right hand class is present in jboss-modules.jar. But then, it is a subclass of the JDK's DocumentBuilderFactory, so it should be working.
                        • my app's module (Spring) uses XML Commons. It also contains DocumentBuilderFactory which overrides JDK's class.
                        • Spring tries to create a factory instance, but it is not compatible with the class it uses: __redirected.__DocumentBuilderFactory parent is from JDK and Spring sees XML Commons

                         

                        Removing XML Commons was not an option as Spring uses its other classes.

                        The solution that worked for me was to declare XML Commons as such:

                         

                        <module xmlns="urn:jboss:module:1.3" name="org.apache.xmlcommons">

                          <resources>

                              <resource-root path="xml-apis.jar">

                                  <filter>

                                      <exclude-set>

                                        <path name="javax" />

                                        <path name="org/xml" />

                                        <path name="org/w3c/dom" />

                                      </exclude-set>

                                  </filter>

                              </resource-root>

                            <resource-root path="xml-apis-ext.jar"/>

                          </resources>

                          <dependencies>

                             <system export="true">

                                <paths>

                                    <path name="javax/xml/parsers" />

                                    <path name="org/xml/sax" />

                                    <path name="org/w3c/dom" />

                                </paths>

                            </system>

                          </dependencies>

                        </module>

                         

                        So XML Commons exposes (exports) only the classes which are not present in JDK. Spring has access to resources it needs and there is not class clash.

                        • 9. Re: __redirected.__DocumentBuilderFactory ClassCastException
                          bpbhat77

                          I am facing same isuue,

                           

                          I m using old version of sping . where to add this xml ?

                          • 10. Re: __redirected.__DocumentBuilderFactory ClassCastException
                            senlog80

                            Hi Marcin Cieślak,

                             

                            I am facing the same kind of issue.

                             

                            19:32:50,881 ERROR [stderr] (default task-1) Caused by: java.lang.ClassCastExcep

                            tion: __redirected.__DatatypeFactory cannot be cast to javax.xml.datatype.Dataty

                            peFactory

                             

                            It seems the class is present in javax.api which is by default loaded by Wildfly.  My global module tries to load xml-api.jar and there is a conflict.  Could you provide your suggestion that how I can overcome this issue.

                             

                            Thanks

                            Senthil

                            • 11. Re: __redirected.__DocumentBuilderFactory ClassCastException
                              trybik03

                              Hi,

                              try putting the XML I wrote into

                              <WildFly dir>/modules/system/layers/base/org/apache/xmlcommons/main/module.xml

                              • 12. Re: __redirected.__DocumentBuilderFactory ClassCastException
                                senlog80

                                Hi Marcin,

                                 

                                 

                                I solved the issue.

                                I have removed the below modules and added javax.api and org.slf4j as dependencies in my module.xml.  It is because wildfly already having these modules and loaded as part of core class loading.  Thats why there is a conflict when it is reloaded.

                                 

                                 

                                Module.xml

                                ----------

                                Removed content :

                                ------------------

                                <resource-root path="xml-apis-<version>.jar">

                                <resource-root path="slf4j-api-<version>.jar"/>

                                 

                                 

                                Added content :

                                ---------------

                                <dependencies>

                                         <module name="javax.api"/>

                                  <module name="org.slf4j"/>

                                  </dependencies>

                                 

                                 

                                Thanks

                                Senthil

                                • 13. Re: __redirected.__DocumentBuilderFactory ClassCastException
                                  zdenek.hrib

                                  I hit the same problem in Wildfly 10.0.1.Final. I had this code in the my Stateless EJB constructor to create the handle for an external web service client call:

                                   

                                  URL wsdlURL = new URL("file:/c:/filename.wsdl");

                                  QName serviceName = new QName("http://www.something.cz/xxx/1234", "ServiceName");

                                  Service service = Service.create(wsdlURL, serviceName);

                                  MyTypePort port = service.getPort(MyTypePort.class);

                                   

                                  when I moved this code into a getter for a class field, it worked, because now it is called during the first business process and not during the bean init.

                                   

                                  The problem was not the code, but when it was called.