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
rest groovy
tinlyx
source share