Details
Description
Trying to obtain Jedis library (i.e. Redis client library - https://github.com/xetorthio/jedis) using following code doesn't work:
StartRedis.groovy
@Grab('redis.clients:jedis:1.3.0')
import redis.clients.jedis.*
Jedis jedis = new Jedis("localhost")
jedis.set("foo", "bar");
println jedis.get("foo");
The 'new' keyword is marked with error. Tooltip says: 'Groovy:unable to resolve class Jedis'. After running this code using 'Run as Java Application' the exception appears:
xception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: No suitable ClassLoader found for grab at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77) at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:52) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:198) at groovy.grape.GrapeIvy.chooseClassLoader(GrapeIvy.groovy:163) at groovy.grape.GrapeIvy$chooseClassLoader.callCurrent(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149) at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:227) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:225) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:51) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:153) at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:216) at groovy.grape.Grape.grab(Grape.java:131) at groovy.grape.Grape$grab.callStatic(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:165) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:173) at RedisConnect.<clinit>(RedisConnect.groovy)
Works smoothly from CLI.
You cannot run Grabby things as a Java Application like that. If you try it on the command line you get the same thing:
groovyc ThatScript.groovy
java ThatScript
will fail with the Grab error.
The classloader setup is only correct if you run it as a script. This will work on the command line:
groovy ThatScript.groovy
which you can also do in Eclipse by using the context menu RunAs>GroovyScript