使用 GroovyScriptEngine 运行 groovy 脚本。这是创建的 groovy 文件。
BasePayCalculation.groovy
return calculate()
def calculate() {
def currentPay = currentPay
currentPay = normalize(currentPay);
// Current pay cannot be zero
if (currentPay == 0) {
throw new IllegalArgumentException("Current pay is zero")
}
}
def normalize(def it) {
if (it == null)
return 0
else
return it
}
从应用程序运行它,得到
java.lang.ClassFormatError: Illegal class name "cds:BasePayCalculation$normalize$0" in class file cds:BasePayCalculation$normalize$0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.define(ClassLoaderForClassArtifacts.java:42)
at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:86)
at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:84)
at java.security.AccessController.doPrivileged(Native Method)
at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.defineClassAndGetConstructor(ClassLoaderForClassArtifacts.java:84)
at org.codehaus.groovy.runtime.callsite.CallSiteGenerator.compilePogoMethod(CallSiteGenerator.java:217)
at org.codehaus.groovy.reflection.CachedMethod.createPogoMetaMethodSite(CachedMethod.java:228)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.createCachedMethodSite(PogoMetaMethodSite.java:212)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.createPogoMetaMethodSite(PogoMetaMethodSite.java:188)
at groovy.lang.MetaClassImpl.createPogoCallCurrentSite(MetaClassImpl.java:3122)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallCurrentSite(CallSiteArray.java:108)
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 cds:BasePayCalculation.calculate(cds:BasePayCalculation.groovy:20)
at cds:BasePayCalculation.run(cds:BasePayCalculation.groovy:16)
at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:551)
请帮我解决这个问题。
来自下面的额外信息:
- 使用 web 应用程序从 tomcat 运行。
- 时髦的 2.1.1。
使用代码运行它
Binding params = new Binding() if (context != null) { for (String key : context.keySet()) { Object param = context.get(key); params.setVariable(key, param); } } long time = System.currentTimeMillis(); Object object = engine.run(name, params);
Java 版本 1.6 tomcat 版本 6.0.24
提前致谢。