Groovy: unable to resolve groovyx.net.http.RESTClient class

I am learning groovy for a scripting package called geoscript-groovy. I followed the groovy REST tutorial here and tested the following code:

import groovyx.net.http.RESTClient def client = new RESTClient( 'http://www.acme.com/' ) def resp = client.get( path : 'products/3322' ) // ACME boomerang 

However, in the import statement, I received an error message:

 Groovy:unable to resolve class groovyx.net.http.RESTClient 

I searched, and there are many questions and answers for this error message, for example import groovyx.net.http.RESTClient in the groovy class , and RestClient Grails import error . However, they are all for the grail, which I do not use and are not very familiar with.

My question

How do I fix this error if I only have groovy? (My version of groovy is installed under Ubuntu 12.04 with the following commands).

 sudo apt-add-repository ppa:groovy-dev/groovy sudo apt-get update sudo apt-get install groovy 

Thanks.

- EDIT ---

I added @Grab statements as suggested, and placed the rest1.groovy file with two lines as follows:

 @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7') import groovyx.net.http.RESTClient 

groovyConsole rest1.groovy seems to be working fine. But groovysh < rest1.groovy still gives me an error (as shown below). I think I need to run this run in a groovysh -like environment because the groovy script is being called in the background as a web service. Without the @Grab line @Grab service throws an exception. Using the @Grab line @Grab service will not even register. Is there a more permanent way to include the necessary dependencies for groovyx.net.http.RESTClient than grabbing from a script (e.g. apt-get or manually copying something)?

 groovysh < rest1.groovy Groovy Shell (1.8.6, JVM: 1.7.0_72) Type 'help' or '\h' for help. ------------------------------------------------------------------------------- groovy:000> @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7') groovy:001> import groovyx.net.http.RESTClient ERROR org.codehaus.groovy.tools.shell.CommandException: Invalid import definition: 'import groovyx.net.http.RESTClient'; reason: startup failed: script1413902882282760571375.groovy: 1: unable to resolve class groovyx.net.http.RESTClient @ line 1, column 1. import groovyx.net.http.RESTClient 
+8
rest groovy
source share
1 answer

You probably just need the Grape line to make sure your Groovy script has the jar that you need in the classpath. Put this at the top of your script:

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

Please note: I do not see the rest of your script, so there may be other modules that you need to capture. Check out additional features here: http://groovy.codehaus.org/modules/http-builder/doc/rest.html

EDIT

Okay, glad it works now. As for groovysh, I don’t know how groovysh would dynamically get the dependent libraries, so what you really need to do, as part of the installation of the script, is also put the jar that you need in the directory (name it β€œlib” or some), and then add an argument to your groovysh call: groovysh -cp./lib <script.groovy from this: http://groovy.codehaus.org/Groovy+Shell

The container you want should be accessible via maven using the artifact specification from the @Grab line.

+12
source

All Articles