Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy ShortTypeHandling ClassNotFoundException

Tags:

groovy

I have a groovy application that uses groovy version 2.2.1. My groovy app was previously running fine but has recently started throwing this exception:

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at com.app.Main.main(Main.groovy:83)Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.runtime.typehandling.ShortTypeHandling
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

The ShortTypeHandling class was not even introduced until groovy 2.3.0. How can it be referenced in a groovy app running version 2.2.1? I can solve this problem by replacing the groovy-all-2.2.1.jar with groovy-all-2.3.0.jar in my pom but that doesn't root cause the issue.

like image 550
GregG Avatar asked May 24 '14 15:05

GregG


3 Answers

ShortTypeHandling was introduced in groovy-all-2.3.0.jar so the quick fix was to replace the older groovy-all-x.x.x.jar with groovy-all-2.3.0.jar. This solved the runtime ShorTypeHandling ClassNotFoundException but also created new problems by introducing a new groovy-all.jar dependency in the application.

The real issue was how the groovy compiler was being invoked via maven. Because I introduced spock which required groovy 2.0, I needed to update the maven entries for the groovy-eclipse-compiler dependency. Here are the updated maven entries for working with groovy 2.x:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <compilerId>groovy-eclipse-compiler</compilerId>
                <!-- Java version -->
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-compiler</artifactId>
                    <version>2.8.0-01</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-batch</artifactId>
                    <!-- Groovy version -->
                    <version>2.1.8-01</version>
                </dependency>
            </dependencies>
        </plugin>

With this in place, I could leave my groovy-all dependency the way I originally had it for the working/fully tested application like this:

    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <!-- If possible, its better if this matches 2.1.8 in the plugin definition -->
        <!-- but 2.2.1 worked fine here and allowed me to keep the original pom definition  -->
        <version>2.2.1</version>
    </dependency>

The application runtime no longer references the ShortTypeHandling class and everything worked as it previously did.

like image 185
GregG Avatar answered Nov 11 '22 16:11

GregG


You have to add (If you are using Gradle)

compile 'org.codehaus.groovy:groovy-backports-compat23:2.4.5'
like image 20
Xelian Avatar answered Nov 11 '22 18:11

Xelian


I've just had this after updating the groovy-eclipse Feature in Eclipse (in order to try and fix intermittent save issues caused by https://jira.codehaus.org/browse/GRECLIPSE-1519). Specifically in my case, my Groovy JUnit tests were throwing this exception.

In light of the suggestions above, I checked my Eclipse settings, and it was using Groovy 2.3.4.xx whereas my Maven POM was specifying 2.1.8.xx. I went to Window -> Preferences -> Groovy -> Compiler and clicked "Switch to 2.1.8.xx...", restarting Eclipse when prompted, and this fixed it.

like image 13
Matthew Wise Avatar answered Nov 11 '22 16:11

Matthew Wise