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, I got an error at the import
statement saying:
Groovy:unable to resolve class groovyx.net.http.RESTClient
I searched around, and there are many questions and answers for this error message, e.g., import groovyx.net.http.RESTClient in Groovy class , and RestClient Grails Import fails. However, they all for grails, which I don't use and am not very familiar with.
My question is
How should I fix this error if I have groovy only? (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 put up a two-line rest1.groovy
file 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 run OK. But groovysh < rest1.groovy
is still giving me an error (as shown below). I guess I need to have this run in a groovysh
-like environment because the groovy script is called in the background as a web service. Without the @Grab
line, the service generates an Exception. With the @Grab
line, the service won't even register. Is there a more permanent way of including necessary dependencies for groovyx.net.http.RESTClient
than a per script grab (e.g. an apt-get
or manual copying of 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
You probably just need the Grape line to properly make sure your Groovy script has the jar 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' )
Note, I can't see the rest of your script, so there may be other modules you need to Grab too. Check here for more possibilities: http://groovy.codehaus.org/modules/http-builder/doc/rest.html
EDIT
Well, glad it works part of the way now. As far as groovysh goes, I don't know of a way to have groovysh dynamically get the dependent libraries, so what you really need to do is, as part of the script install, also put the jar you need in a directory (call it "lib" or some such), and then add the argument to your groovysh call: groovysh -cp ./lib < script.groovy from this: http://groovy.codehaus.org/Groovy+Shell
The jar you want should be available via maven using the artifact specification from the @Grab line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With