34

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.

GregG
  • 781
  • 1
  • 6
  • 10
  • I just started playing with Grails 2.4 which uses groovy 2.3.0, and ran across a ShortTypeHandling ClassNotFoundException when I moved the project over to use the Maven plug-in. This is my first time coming across this issue as well and I haven't quite identified the source. I suspect maybe groovy 2.3.0 is coming in through one of your plugin dependencies? Have you added a new one or upgraded one recently? What version of the maven plugin are you using? – david May 24 '14 at 15:40
  • I thought that it had to be a mvn dependency bringing in the groovy-all-2.3.0 jar as well. I did a mvn dependency tree on the entire pom (mvn dependency:tree -Dverbose) and didn't see any reference to groovy 2.3.0. One last thing, the app works great when it runs inside Intellj (which has no groovy 2.3.0 dependencies). The problem only surfaces when I compile/package/run via mvn command line. – GregG May 24 '14 at 18:47
  • Sorry, you asked about versions, I'm using the groovy-eclipse-compiler v2.7.0. The only new plugin that has been added since the app stopped working is a test code coverage plugin, cobertura-maven-plugin v2.6 – GregG May 24 '14 at 18:56

7 Answers7

22

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.

GregG
  • 781
  • 1
  • 6
  • 10
  • 1
    You might find Guillaume's release announcement helpful: http://groovy.329449.n5.nabble.com/ANN-Groovy-2-3-5-released-and-upward-compatibility-td5720557.html. They introduced a compatibility jar to add this class. – Keegan Jul 25 '14 at 18:49
20

You have to add (If you are using Gradle)

compile 'org.codehaus.groovy:groovy-backports-compat23:2.4.5'
facundofarias
  • 2,973
  • 28
  • 27
Xelian
  • 16,680
  • 25
  • 99
  • 152
13

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.

Matthew Wise
  • 2,639
  • 26
  • 23
9

I've solved this issue by adding this dependency on my POM:

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-backports-compat23</artifactId>
  <version>2.4.5</version>
</dependency>

Then, it works like a charm.

facundofarias
  • 2,973
  • 28
  • 27
2

Matthew Wise's solution worked for me, but in addition to restarting eclipse, I also had to do a project -> clean for it to recompile with the new compiler.

(I would have commented on his answer, but stack overflow has this stupid rule that you can't comment until you get more reputation)

AForsberg
  • 1,074
  • 2
  • 13
  • 25
1

I faced similar issue in our project. Surprisingly groovy version was not an issue. I was building the project with older version of gradle than the expected gradle version for the project. That resolved the error.

Sanjay Bharwani
  • 3,317
  • 34
  • 31
0

Add following dependency to your pom.xml

     <dependency>
        <groupId>org.codehaus.groovy.maven.runtime</groupId>
        <artifactId>gmaven-runtime-default</artifactId>
        <version>1.0-rc-3</version>
        <exclusions>
            <exclusion>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Kuldeep Singh

Kuldeep Singh
  • 199
  • 1
  • 11