Use ScriptEngineManager to execute groovy++ code

55 views
Skip to first unread message

liulei

unread,
Oct 14, 2011, 2:10:26 AM10/14/11
to Groovy++
I use below code to execute groovy++ code:

public class ScriptRun {

public static void main(String[] s) {

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("groovy");
File source = new File("D:/study2011-10-09/groovy-project/src/
file/WordCount.groovy");

try {
engine.eval(new FileReader(source));
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (ScriptException e) {

e.printStackTrace();
}
}
}


Below content is the groovy++ code:
@Typed package file

for (i in 0..<5) {
Long t1 = System.currentTimeMillis()
def counts = [:]
new File("./20_newsgroups").eachFileRecurse{ f ->
if (!f.isDirectory() && !f.path.contains(".svn")) {

f.text.toLowerCase().eachMatch(/\w+/) { w ->
int c = counts[w] ?: 0
counts[w] = c + 1
}
}
}



new File("counts-descreasing-groovy").withWriter { out ->
counts.sort { a, b -> b.value <=> a.value }.each { k, v -> out << "$k
\t$v\n"}
}

new File("counts-alphabetical-groovy").withWriter { out ->
counts.sort { it.key }.each { k, v ->
out << "$k\t$v\n"
}
}

println "Finished in ${System.currentTimeMillis() - t1} millis"
}



When I run ScriptRun class, the class throw below Exception:

Caused by:
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed:
Script1.groovy: -1: Internal Error: java.lang.NoSuchMethodError:
org.codehaus.groovy.classgen.BytecodeInstruction.visit(Lorg/objectweb/
asm/MethodVisitor;)V
@ line -1, column -1.
1 error

at
org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:
302)
at
org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:
984)
at
org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:
542)
at
org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:
520)
at
org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:
497)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:
306)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:
287)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:
267)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:
214)
at
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:
337)
at
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:
109)
... 3 more

How can I use Java Script API to execute groovy++ code?


Thanks,

LiuLei

Alex Tkachman

unread,
Oct 14, 2011, 2:39:58 AM10/14/11
to groovyp...@googlegroups.com
What versions of Groovy and Groovy++ do you use?

lei liu

unread,
Oct 15, 2011, 11:37:16 AM10/15/11
to groovyp...@googlegroups.com

Groovy 1.8.2 and groovypp-0.9.0_1.8.2



2011/10/14 Alex Tkachman <alex.t...@gmail.com>

Alex Tkachman

unread,
Oct 15, 2011, 11:46:44 AM10/15/11
to groovyp...@googlegroups.com
What jars do you have in your classpath? Chech that both either have or have not -all suffix
Reply all
Reply to author
Forward
0 new messages