将Groovy 2.1.7与JDK 1.7.0_40一起使用。
我正在尝试编写一个非常简单的原型groovy脚本。它将调用RESTClient并处理服务的结果。
它看起来像这样:
#! env groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' )
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1' )
import groovyx.net.http.RESTClient;
println("About to create RESTClient.");
service = new RESTClient("http://myhost/")
def diagnostics = service.get(path: "mypath");
println("diagnostics[" + diagnostics + "]");
当我运行时,我明白了:
About to create RESTClient.
Caught: java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
at monitorStatus.run(monitorStatus.groovy:9)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
... 1 more
我看到有人提到需要在方法上加上“@Grab”注释。我已经尝试了一些方法的注释,但它没有区别。
我在这里做错了什么?
修改 我将“-Dgroovy.grape.report.downloads = true”添加到我的JAVA_OPTS中并对脚本进行了一些微不足道的更改。
这是我当前的脚本,只修改了网址和路径:
#! /usr/bin/env groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' )
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.2.5' )
import groovyx.net.http.RESTClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpUriRequest;
println("About to create RESTClient.");
service = new RESTClient("http:///")
def diagnostics = service.get(path: "mypath");
println("diagnostics[" + diagnostics + "]");
这是我运行时得到的结果:
Resolving dependency: org.apache.httpcomponents#httpclient;4.2.5 {default=[default]}
Resolving dependency: org.codehaus.groovy.modules.http-builder#http-builder;0.5.2 {default=[default]}
Preparing to download artifact org.apache.httpcomponents#httpclient;4.2.5!httpclient.jar
Preparing to download artifact org.codehaus.groovy.modules.http-builder#http-builder;0.5.2!http-builder.jar
Preparing to download artifact org.apache.httpcomponents#httpcore;4.2.4!httpcore.jar
Preparing to download artifact commons-logging#commons-logging;1.1.1!commons-logging.jar
Preparing to download artifact commons-codec#commons-codec;1.6!commons-codec.jar
Preparing to download artifact net.sf.json-lib#json-lib;2.3!json-lib.jar
Preparing to download artifact org.codehaus.groovy#groovy;1.7.11!groovy.jar
Preparing to download artifact net.sourceforge.nekohtml#nekohtml;1.9.9!nekohtml.jar
Preparing to download artifact xml-resolver#xml-resolver;1.2!xml-resolver.jar
Preparing to download artifact commons-beanutils#commons-beanutils;1.8.0!commons-beanutils.jar
Preparing to download artifact commons-collections#commons-collections;3.2.1!commons-collections.jar
Preparing to download artifact commons-lang#commons-lang;2.4!commons-lang.jar
Preparing to download artifact net.sf.ezmorph#ezmorph;1.0.6!ezmorph.jar
Preparing to download artifact antlr#antlr;2.7.7!antlr.jar
Preparing to download artifact asm#asm;3.2!asm.jar
Preparing to download artifact asm#asm-commons;3.2!asm-commons.jar
Preparing to download artifact asm#asm-util;3.2!asm-util.jar
Preparing to download artifact asm#asm-analysis;3.2!asm-analysis.jar
Preparing to download artifact asm#asm-tree;3.2!asm-tree.jar
Preparing to download artifact xerces#xercesImpl;2.8.1!xercesImpl.jar
Preparing to download artifact xml-apis#xml-apis;1.3.03!xml-apis.jar
About to create RESTClient.
Caught: java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
at monitorSunlightDataSourceStatus.run(monitorSunlightDataSourceStatus.groovy:10)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
... 1 more
这是否提供了有用的线索?
答案 0 :(得分:3)
尝试删除~/.m2
目录以及~/.groovy/grapes directory
它对我有用。
答案 1 :(得分:0)
好吧,我设法解决了这个问题,但我不确定问题是什么。 groovy-user列表中的某个人建议删除〜/ .groovy并重新安装Groovy。我首先尝试了前者,这只会使脚本运行得更慢,但它仍然以同样的方式失败。然后我尝试重新安装Groovy并删除〜/ .groovy,之后脚本能够成功执行。