2008-10-10 72 views
12

我試過在Groovy 1.6-beta-2中使用新的Groovy Grape功能,但我收到一條錯誤消息;獲取Groovy的葡萄!

unable to resolve class com.jidesoft.swing.JideSplitButton 

從Groovy的控制檯(/opt/groovy/groovy-1.6-beta-2/bin/groovyConsole)運行庫存例如當;

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 
    } 
} 

我甚至嘗試運行grape命令行工具來確保庫已導入。喜歡這個;

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

哪個安裝庫就好了。我如何從groovyConsole中正確運行/編譯代碼?

回答

5

在制定啓動/終止開關程序方面還存在一些問題。對於β-2這樣做在自己的腳本首先:

groovy.grape.Grape.initGrape() 

另外一個問題,你會碰到與使用無界上限的樂趣交易。從2.3.0開始,Jide-oss一直在將它們的代碼編譯爲Java 6字節碼,因此您需要在Java 6中運行控制檯(無論如何您都希望這樣做),或者設置上限範圍,像這樣

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() 
2

好的。看起來這很短的工作演示(從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 

運行時它產生

結果: 「com.jidesoft.swing.JideSplitButton」

非常酷!

+0

這對我很好。空課是關鍵。幾點... 1 /我不需要initGrape()行(使用groovy 1.7x或1.8x).. 2 /如果您在代理後面請確保添加您的代理設置-Dhttp.proxyHost = -Dhttp.proxyPort = ...我將它們添加到我的開始處Groovy.sh | bat – khylo 2012-03-05 09:31:38

-1
採用最新RC-2

不同的例子(注:抓住詮釋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 
+1

從http://groovy.codehaus.org/Grape中盜取的示例。 (完全複製) 此外,它不回答問題。 – 2009-04-23 20:09:07

+1

我同意。如果您在網絡上的其他位置找到正確答案,請向他們推薦。提供一個鏈接。請不要在沒有歸屬的情況下爲其他人的工作留下好印象。 – Anarchofascist 2013-07-15 23:28:55

-1

另一個例子(注:抓住詮釋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 } 
+0

從http://groovy.codehaus.org/Grape中盜取的示例。 (完全複製) 此外,它不回答問題。 – 2009-04-23 20:10:12

-3

另一個例子(注:Grab詮釋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' 
5

我終於搞定了fo r Groovy Shell(1.6.5,JVM:1.6.0_13)。這應該記錄得更好。

首先在命令行...

葡萄安裝org.codehaus.groovy.modules.http建設者HTTP建設者0.5.0-RC2

然後在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') 
===> [email protected] 

@grab比shell更適合在文件中使用。

0

導入語句必須在之後出現
Ps。至少一個進口聲明必須存在之後

@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 
    } 
}