groovy

In Groovy, How do I fix this error: groovy.util.slurpersupport.GPathResult


Given the following code snippet:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')

def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org'); // <<< THROWS EXCEPTION
println http

How can I fix the following error?

Caught: java.lang.NoClassDefFoundError: groovy/util/slurpersupport/GPathResult
java.lang.NoClassDefFoundError: groovy/util/slurpersupport/GPathResult
    at java.desktop/com.sun.beans.introspect.MethodInfo.get(MethodInfo.java:70)
    at java.desktop/com.sun.beans.introspect.ClassInfo.getMethods(ClassInfo.java:80)
    at groovyx.net.http.ParserRegistry.<init>(ParserRegistry.java:87)
    at groovyx.net.http.HTTPBuilder.<init>(HTTPBuilder.java:194)
    at HttpBuilder.run(HttpBuilder.groovy:4)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Caused by: java.lang.ClassNotFoundException: groovy.util.slurpersupport.GPathResult
    ... 8 more

Versions of Groovy/Java/Gradle/Maven I'm using

I'm using Groovy 4.0 with JDK 17.0.2 as shown next:

groovy --version
Groovy Version: 4.0.0 JVM: 17.0.2 Vendor: Oracle Corporation OS: Windows 10

java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

gradle --version

------------------------------------------------------------
Gradle 7.4
------------------------------------------------------------

Build time:   2022-02-08 09:58:38 UTC
Revision:     f0d9291c04b90b59445041eaa75b2ee744162586

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          17.0.2 (Oracle Corporation 17.0.2+8-LTS-86)
OS:           Windows 10 10.0 amd64

Searching for solutions

Background on question

The UDEMY Groovy course https://www.udemy.com/course/apache-groovy/ provides some sample code during one of the lectures on a REST-based client in Groovy that uses the http-builder library.

The groovy and java version being used at the time of the course where

Java Version: 1.8.0_60
Groovy Version: 2.4.5
Gradle: 2.7
Maven: 3.3.3
Spring Boot: 3.0.M5
Spring Tool Suite (STS): 3.7.1.RELEASE-e4.5.1
On Windows 7 and Mac OS ?

Solution

  • i'm sure you have new version of groovy (for example 4.0.1) and http-builder library you are using is quite old.

    starting from groovy 3.0 GPathResult class moved to another package: groovy.xml.slurpersupport.GPathResult

    but according to error HTTPBuilder is looking for old package groovy.util.slurpersupport.GPathResult


    option 1: You have to downgrade groovy version


    option 2: hacking

    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
    import groovyx.net.http.*
    
    //define empty class with old name to prevent failure
    this.getClass().getClassLoader().getParent().parseClass '''
      package groovy.util.slurpersupport
      class GPathResult{}
    '''  
    
    def http = new HTTPBuilder('http://httpbin.org')
    //redefine xml parser to use xml slurper from a new package
    //you don't need this if you are not going to work with xml
    http.parser['application/xml']={HttpResponseDecorator r-> 
        return new groovy.xml.XmlSlurper().parse(r.entity.content)
    }
    http.get(path:'/xml',query:[a:123]){resp,body->
      println "status: ${resp.statusLine}"
      println groovy.xml.XmlUtil.serialize(body)
    }