QA@IT

Grapeで依存性解決をしているGroovyスクリプトをコンパイルしてjavaコマンドで実行する方法

3835 PV

groovycして実行しても以下のようなエラーが出てしまいます。

$ groovyc Twitter4jSample.groovy 
$ java -cp .:/Users/eiryu/.gvm/groovy/current/embeddable/groovy-all-2.2.1.jar Twitter4jSample
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
    at java.lang.Class.getDeclaredMethods(Class.java:1855)
    at org.codehaus.groovy.reflection.CachedClass$3$1.run(CachedClass.java:84)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue(CachedClass.java:81)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue(CachedClass.java:79)
    at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
    at org.codehaus.groovy.reflection.CachedClass.getMethods(CachedClass.java:250)
    at groovy.lang.MetaClassImpl.populateMethods(MetaClassImpl.java:342)
    at groovy.lang.MetaClassImpl.fillMethodIndex(MetaClassImpl.java:292)
    at groovy.lang.MetaClassImpl.initialize(MetaClassImpl.java:3045)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:176)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:192)
    at groovy.grape.GrapeIvy.$getStaticMetaClass(GrapeIvy.groovy)
    at groovy.grape.GrapeIvy.<init>(GrapeIvy.groovy:76)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at groovy.grape.Grape.getInstance(Grape.java:117)
    at groovy.grape.Grape.grab(Grape.java:140)
    at groovy.grape.Grape$grab.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:157)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:165)
    at Twitter4jSample.<clinit>(Twitter4jSample.groovy)
Caused by: java.lang.ClassNotFoundException: org.apache.ivy.core.report.ResolveReport
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 29 more

以下も試してみましたがうまくいきませんでした。

  • javaコマンド実行時のクラスパスにivyを追加

    java -cp .:/Users/eiryu/.gvm/groovy/current/embeddable/groovy-all-2.2.1.jar:~/.groovy/grapes/org.apache.ivy/ivy/jars/ivy-2.3.0.jar Twitter4jSample
    
  • groovycコマンド実行時のクラスパスにivyを追加

    groovyc -cp ~/.groovy/grapes/org.apache.ivy/ivy/jars/ivy-2.3.0.jar Twitter4jSample.groovy
    

ソースファイル(Twitter4jSample.groovy)

@Grab("org.twitter4j:twitter4j-core:3.0.5")
import twitter4j.Twitter;
import twitter4j.TwitterFactory;

Twitter tw = TwitterFactory.getSingleton()
tw.getUserTimeline().each{ println it.getText() }

追記 2014/03/23

ivyをGrape管理のものではなく、GROOVY_HOME/libのものにしたらエラーが変わりました。

$ java -cp .:/Users/eiryu/.gvm/groovy/current/lib/ivy-2.3.0.jar:/Users/eiryu/.gvm/groovy/current/embeddable/groovy-all-2.2.1.jar Twitter4jSample
Exception 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:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    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:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
    at groovy.grape.GrapeIvy.chooseClassLoader(GrapeIvy.groovy:178)
    at groovy.grape.GrapeIvy$chooseClassLoader.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
    at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:244)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:231)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:52)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145)
    at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:233)
    at groovy.grape.Grape.grab(Grape.java:148)
    at groovy.grape.Grape$grab.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:157)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:165)
    at Twitter4jSample.<clinit>(Twitter4jSample.groovy)

回答

Twitterで @kazuhira_r さんや @grimrose さんに教えていただいたのですが、どうもGrapeがAST変換していることが原因?のようです。
GroovyConsoleで ’Inspect AST’ したソースを以下に載せておきます。

import twitter4j.TwitterFactory as TwitterFactory
@groovy.lang.Grab(module = 'twitter4j-core', group = 'org.twitter4j', version = '3.0.5')
import twitter4j.Twitter as Twitter

public class script1395850804763 extends groovy.lang.Script { 

    public script1395850804763() {
    }

    public script1395850804763(groovy.lang.Binding context) {
        super.setBinding(context)
    }

    public static void main(java.lang.String[] args) {
        org.codehaus.groovy.runtime.InvokerHelper.runScript(script1395850804763, args)
    }

    public java.lang.Object run() {
        twitter4j.Twitter tw = twitter4j.TwitterFactory.getSingleton()
        tw.getUserTimeline().each({ 
            this.println(it.getText())
        })
    }

    static { 
        groovy.grape.Grape.grab(['group': 'org.twitter4j', 'module': 'twitter4j-core', 'version': '3.0.5'])
    }

}
編集 履歴 (0)
ウォッチ

この質問への回答やコメントをメールでお知らせします。