Missing classes of HttpBuilder in Groovy

738 views Asked by At

Here is simple request I try to do and watch response

def httpReq = new HTTPBuilder("http://${url}")
        httpReq.request(Method.GET, ContentType.JSON) {

            response.success = { resp ->
                println("Ok  " + resp.status.toString())
            }

            response.failure = { resp ->
                println("Not ok" + resp.status.toString())
            }

This returns an Error:

java.lang.ClassNotFoundException: org.apache.commons.collections.iterators.ArrayIterator

I think that ArrayIterator defined in jdk. I added manually org.apache.commons:commons-collections4:4.4 but it doesn't help.

1

There are 1 answers

2
cfrick On

TL;DR

Don't waste your time by gathering deps. Use a build tool like Gradle or Maven - or use Groovys Grapes if only a script is needed and Groovy is installed (or easily installable) on your target.

Using grape

You have to pick up all the transitive dependencies (this means the correct packages with the correct versions). E.g. with @Grab:

@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
import groovyx.net.http.HTTPBuilder

def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )

If you really want to gather all the deps manually grape can also help. It can tell you, what jars are needed:

% grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.1
$HOME/.groovy/grapes/org.codehaus.groovy.modules.http-builder/http-builder/jars/http-builder-0.7.1.jar
$HOME/.groovy/grapes/org.apache.httpcomponents/httpclient/jars/httpclient-4.2.1.jar
$HOME/.groovy/grapes/org.apache.httpcomponents/httpcore/jars/httpcore-4.2.1.jar
$HOME/.groovy/grapes/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar
$HOME/.groovy/grapes/commons-codec/commons-codec/jars/commons-codec-1.6.jar
$HOME/.groovy/grapes/net.sf.json-lib/json-lib/jars/json-lib-2.3-jdk15.jar
$HOME/.groovy/grapes/commons-beanutils/commons-beanutils/jars/commons-beanutils-1.8.0.jar
$HOME/.groovy/grapes/commons-collections/commons-collections/jars/commons-collections-3.2.1.jar
$HOME/.groovy/grapes/commons-lang/commons-lang/jars/commons-lang-2.4.jar
$HOME/.groovy/grapes/net.sf.ezmorph/ezmorph/jars/ezmorph-1.0.6.jar
$HOME/.groovy/grapes/net.sourceforge.nekohtml/nekohtml/jars/nekohtml-1.9.16.jar
$HOME/.groovy/grapes/xerces/xercesImpl/jars/xercesImpl-2.9.1.jar
$HOME/.groovy/grapes/xml-apis/xml-apis/jars/xml-apis-1.3.04.jar
$HOME/.groovy/grapes/xml-resolver/xml-resolver/jars/xml-resolver-1.2.jar

From this list you can build up your own classpath to pass to groovy and work without grape. E.g.:

% cat no-grape.groovy 
import groovyx.net.http.HTTPBuilder

def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )

% groovy -cp `grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.1 | paste -s -d:` no-grape.groovy
...