'How to resolve: antlr.CommonToken cannot be cast to antlr.Token?

I have just finished an update from wildfly 9 to 23 and am now re-working my arquillian implementation to work with the new application server version.

Dependency management:

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.4.1.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>wildfly-jakartaee8-with-tools</artifactId>
                <scope>import</scope>
                <type>pom</type>
                <version>21.0.2.Final</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

Dependencies:

        <dependency>
            <groupId>org.wildfly.arquillian</groupId>
            <artifactId>wildfly-arquillian-container-embedded</artifactId>
            <version>3.0.1.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <version>${arquillian.version}</version>
            <scope>test</scope>
        </dependency>

When I run the tests I get this error:

[ERROR] org.<company name>.crs.auth.CrsUserServiceTest  Time elapsed: 71.828 s  <<< ERROR!
org.jboss.arquillian.container.spi.client.container.DeploymentException: 
Cannot deploy ert-tests.war: {"WFLYCTL0062: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-1" => {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"ert-tests.war#ertDatasource\"" => "javax.persistence.PersistenceException: [PersistenceUnit: ertDatasource] Unable to build Hibernate SessionFactory
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: ertDatasource] Unable to build Hibernate SessionFactory
    Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.collection.OneToManyPersister
    Caused by: org.hibernate.HibernateException: Unable to parse order-by fragment
    Caused by: java.lang.ClassCastException: antlr.CommonToken cannot be cast to antlr.Token"}}}}

After researching online, many people have said this is due to conflicting versions of antlr (frequently a manually imported one and one shipped with hibernate). Here is my hibernate import:

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.20.Final</version>
            <scope>provided</scope>

            <exclusions>
                <exclusion>
                    <groupId>antlr</groupId>
                    <artifactId>antlr</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.dom4j</groupId>
                    <artifactId>dom4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Which explicitly does not bring antlr with. In fact, if I run mvn dependency:tree and search for antlr i get no results. So, not sure how I have conflicting versions if I don't have any in the class path or imported at all.

I was able to find This article mentioning that It could be due to multiple application's running. But not sure if this applies since I only have a single app running and only one instance of it(i think). While launching arquillian i get this message from Wildfly 15:50:35,940 INFO [io.jaegertracing.internal.JaegerTracer] (MSC service thread 1-4) No shutdown hook registered: Please call close() manually on application shutdown. But I get that message twice when I run the tests. Wondering if this could be causing the issue and if not, what else could be?



Solution 1:[1]

Setting JAVA_OPTS as an env-var with the value of -DANTLR_USE_DIRECT_CLASS_LOADING=true helped me to solve this issue. Leaving here as well a screenshot for config in IntelliJ:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pascal