13

I've tried to use the new Groovy Grape capability in Groovy 1.6-beta-2 but I get an error message;

unable to resolve class com.jidesoft.swing.JideSplitButton

from the Groovy Console (/opt/groovy/groovy-1.6-beta-2/bin/groovyConsole) when running the stock example;

import com.jidesoft.swing.JideSplitButton
@Grab(group='com.jidesoft', module='jide-oss', version='[2.2.1,)')
public class TestClassAnnotation {
    public static String testMethod () {
        return JideSplitButton.class.name
    }
}

I even tried running the grape command line tool to ensure the library is imported. Like this;

 $ /opt/groovy/groovy-1.6-beta-2/bin/grape install com.jidesoft jide-oss

which does install the library just fine. How do I get the code to run/compile correctly from the groovyConsole?

0

7 Answers 7

5

There is still some kinks in working out the startup/kill switch routine. For Beta-2 do this in it's own script first:

groovy.grape.Grape.initGrape()

Another issue you will run into deals with the joys of using an unbounded upper range. Jide-oss from 2.3.0 onward has been compiling their code to Java 6 bytecodes, so you will need to either run the console in Java 6 (which is what you would want to do for Swing anyway) or set an upper limit on the ranges, like so

import com.jidesoft.swing.JideSplitButton

@Grab(group='com.jidesoft', module='jide-oss', version='[2.2.1,2.3.0)')
public class TestClassAnnotation {
    public static String testMethod () {
        return JideSplitButton.class.name
    }
}

new TestClassAnnotation().testMethod()
5

I finally got it working for Groovy Shell (1.6.5, JVM: 1.6.0_13). This should be documented better.

First at the command line...

grape install org.codehaus.groovy.modules.http-builder http-builder 0.5.0-RC2

Then in groovysh...

groovy:000> import groovy.grape.Grape
groovy:000> Grape.grab(group:'org.codehaus.groovy.modules.http-builder', module:'http-builder', version:'0.5.0-RC2')
groovy:000> def http= new groovyx.net.http.HTTPBuilder('http://rovio')
===> groovyx.net.http.HTTPBuilder@91520

The @grab is better used in a file than the shell.

2

Ok. Seems like this a short working demo (running from the groovyConsole)

groovy.grape.Grape.initGrape()
@Grab(group='com.jidesoft', module='jide-oss', version='[2.2.1,2.3.0)')
public class UsedToExposeAnnotationToComplier {}
com.jidesoft.swing.JideSplitButton.class.name

When run it produces

Result: "com.jidesoft.swing.JideSplitButton"

Very cool!!

1
  • This worked great for me. The empty class is key. Couple of points... 1/ I didn't need the initGrape() line (using groovy 1.7x or 1.8x).. 2/ If you are behind a proxy make sure to add your proxy settings -Dhttp.proxyHost=<proxyhost> -Dhttp.proxyPort=<proxyPort>... I add them to my startGroovy.sh|bat
    – khylo
    Mar 5, 2012 at 9:31
0

The import statement must appear after the grabs.
Ps. At least one import statement must exists after the grabs

@Grab(group='com.jidesoft', module='jide-oss', version='[2.2.1,)')
import com.jidesoft.swing.JideSplitButton
public class TestClassAnnotation {
    public static String testMethod () {
        return JideSplitButton.class.name
    }
}
-1

Different example using latest RC-2 (note: Grab annotates createEmptyInts):

// create and use a primitive array
import org.apache.commons.collections.primitives.ArrayIntList

@Grab(group='commons-primitives', module='commons-primitives', version='1.0')
def createEmptyInts() { new ArrayIntList() }

def ints = createEmptyInts()
ints.add(0, 42)
assert ints.size() == 1
assert ints.get(0) == 42
2
  • 1
    Examples stolen from groovy.codehaus.org/Grape. (copied exactly) Also, it does not answer the question. Apr 23, 2009 at 20:09
  • 1
    I agree. Please refer people to correct answers if you find them elsewhere on the web. Provide a link. Please don't take credit for other people's work without attribution. Jul 15, 2013 at 23:28
-1

Another example (note: Grab annotates getHtml):

// find the PDF links in the Java 1.5.0 documentation
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='0.9.7')
def getHtml() {
    def parser = new XmlParser(new org.ccil.cowan.tagsoup.Parser())
    parser.parse("http://java.sun.com/j2se/1.5.0/download-pdf.html")
}
html.body.'**'[email protected](~/.*\.pdf/).each{ println it }
1
-3

Another example (note: Grab annotates getFruit):

// Google Collections example
import com.google.common.collect.HashBiMap
@Grab(group='com.google.code.google-collections', module='google-collect', version='snapshot-20080530')
def getFruit() { [grape:'purple', lemon:'yellow', orange:'orange'] as HashBiMap }
assert fruit.inverse().yellow == 'lemon'
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.