使用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)
请帮我解决此问题。
使用代码
运行此代码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
提前致谢。
答案 0 :(得分:0)
问题是groovy声明了一个与运行的脚本同名的类,因此脚本名称为needs to be a valid identifier from JVM perspective。在这种情况下,我认为,问题是“:”字符。