1

I am facing a weird issue: when I deploy my WAR file on WildFly I get the following error:

[2017-11-27 01:15:17,777] Artifact JSFDemo:war: Artifact is being deployed, please wait... Connected to server
[2017-11-27 01:15:18,427] Artifact JSFDemo:war: Error during artifact deployment. See server log for details.
[2017-11-27 01:15:18,428] Artifact JSFDemo:war: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"JSFDemo.war#demo_unit\"" => "java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory Caused by: java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory"}}

I believe the problem is related to one of my dependencies. WAR is built on POM file with depedencies inherited from the parent one which is shown below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>org.abondar.experimental</groupId>
<artifactId>JavaEEDemo</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
    <module>BasicCDI</module>
    <module>BeanValidation</module>
    <module>ORMDemo</module>
    <module>EJBDemo</module>
    <module>JSFDemo</module>
    <module>DocumentDemo</module>
</modules>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.1.14.Final</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se-core</artifactId>
        <version>3.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.14.1.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.14.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.12.Final</version>
        <exclusions>
            <exclusion>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.12.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.5.Final</version>
     </dependency>

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.protocol</groupId>
        <artifactId>arquillian-protocol-servlet</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.container</groupId>
        <artifactId>arquillian-container-test-api</artifactId>
        <version>1.1.14.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.jboss.arquillian.core/arquillian-core-impl-base -->
    <dependency>
        <groupId>org.jboss.arquillian.core</groupId>
        <artifactId>arquillian-core-impl-base</artifactId>
        <version>1.1.14.Final</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.1.2</version>
    </dependency>
</dependencies>
<build>
    <!--for web related demos-->
    <plugins>
        <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>1.2.1.Final</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

That's the child POM file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>JavaEEDemo</artifactId>
        <groupId>org.abondar.experimental</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <artifactId>JSFDemo</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <jdkToolchain>
                        <version>[1.8,9)</version>
                    </jdkToolchain>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <skip>false</skip>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <warName>JSFDemo</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>1.2.1.Final</version>
                <configuration>
                    <hostname>localhost</hostname>
                    <port>9990</port>
                    <username>user</username>
                    <password>password</password>
                    <filename>${project.build.finalName}.war</filename>
                </configuration>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I have tried making hibernate-core scope provided and exclude dom4j from it. What is the exact workaround with my POM structure?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Alex Bondar
  • 1,167
  • 4
  • 18
  • 35
  • 1
    Possible duplicate of [wildfly 10: java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory](https://stackoverflow.com/questions/35533847/wildfly-10-java-lang-classcastexception-org-dom4j-documentfactory-cannot-be-ca) – SynchroDynamic Nov 27 '17 at 00:56
  • Not exaclty. The workaround from that question doesn't work. And there is no parent pom like in my case – Alex Bondar Nov 27 '17 at 12:27

2 Answers2

0

It's important to understand what libraries and APIs are provided by your server implementation and the Java runtime.

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <!-- WildFly does not yet implement Java EE 8 -->
    <version>7.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <!-- 
        Either completely remove this or make it "test" scoped
        if it's used by test code
      -->
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-core</artifactId>
    <version>3.0.1.Final</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <!-- always test scope this library -->
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.14.1.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.14.1.0</version>
    <!-- probably should be test scope -->
    <scope>test</scope>
</dependency>
<dependency>
    <!-- Completely remove this -->
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.12.Final</version>
    <exclusions>
        <exclusion>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <!--
         Completely remove this. It's included in javaee-api.
      -->
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>
<dependency>
    <!-- Completely remove this -->
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.5.Final</version>
    <!-- Completely remove unless needed for compilation -->
    <scope>provided</scope>
 </dependency>

<dependency>
    <!--
         Completely remove this. It's included in javaee-api.
      -->
    <groupId>org.glassfish</groupId>
    <artifactId>javax.el</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.protocol</groupId>
    <artifactId>arquillian-protocol-servlet</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.container</groupId>
    <artifactId>arquillian-container-test-api</artifactId>
    <version>1.1.14.Final</version>
    <!-- add missing scope -->
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.arquillian.core/arquillian-core-impl-base -->
<dependency>
    <groupId>org.jboss.arquillian.core</groupId>
    <artifactId>arquillian-core-impl-base</artifactId>
    <version>1.1.14.Final</version>
    <scope>test</scope>
</dependency>


<dependency>
    <!--
         Completely remove this. It's included in java api.
         Version 2.3.0 is Java 9
      -->
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <!--
         Completely remove this. It's included in java 8.
      -->
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <!--
         Completely remove this. It's included in java 8.
      -->
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <!--
         Completely remove this. It's included in javaee-api.
      -->
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <!--
         Completely remove this. It's included in javaee-api.
      -->
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1.2</version>
</dependency>
Steve C
  • 18,876
  • 5
  • 34
  • 37
  • I will leave json and jax-b because my code doesn't built on java 9 without these depdencies – Alex Bondar Nov 27 '17 at 12:31
  • You have the compiler plugin configured for Java 8, so your JAXB code will break at runtime if you're running with Java 8. Additionally, package `javax.json` is included in `javaee-api`. If you're getting compile errors without then you your application will not run in WildFly, because it does not implement the additional Java EE 8 JSON APIs. – Steve C Nov 27 '17 at 13:40
0

The reason you get this error is because you have the class org.dom4j.DocumentFactory loaded from two different classloaders.

Most likely it is provided in WildFly and you packaged it in a JAR in the lib-directory of your WAR file. So look into the WAR and change your POM until it is not included anymore.

To find out why it is included, mvn dependency:tree within your war-module can help.

If you can't figure it out from looking at the WAR-file, set logging of the category org.jboss.modules to TRACE and search the logfile which classloaders load the class org.dom4j.DocumentFactory.

Erhard Siegl
  • 557
  • 2
  • 8