3

Running the groovy script using the GroovyScriptEngine. Here is the groovy file created.

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
}

Running this from the application, getting

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)

Please help me to resolve this issue.

Extra info from below:

  • Running from tomcat using webapplication.
  • Groovy 2.1.1.
  • Running this using code

    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 Version 1.6 tomcat version 6.0.24

Thanks in advance.

5
  • How are you running this? (can you post the code?) What version of Groovy? Commented Mar 6, 2013 at 16:05
  • Running from tomcat using webapplication. Groovy 2.1.1. Running this using '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);' Commented Mar 6, 2013 at 16:06
  • Can you add a bit more code from how are you running? I tried running this using GroovyShell and it worked fine Commented Mar 6, 2013 at 20:42
  • I'm calling the groovy script using GroovyScriptEngine. Engine.run(groovyname, bindingparams); Let me know what more information is needed. Thanks Commented Mar 7, 2013 at 8:54
  • @Raj, did you tried/can you try running outside tomcat? I made a example using scripts and it worked fine. Commented Mar 7, 2013 at 12:16

1 Answer 1

1

The problem is that groovy declares a class with the same name as the script that is run and therefore the script name needs to be a valid identifier from JVM perspective. In this case I presume, the problem is the ":" character.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.