Raj
Raj

Reputation: 71

calling a method from another method is throwing java.lang.ClassFormatError: Illegal class name

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:

Java Version 1.6 tomcat version 6.0.24

Thanks in advance.

Upvotes: 3

Views: 2441

Answers (1)

Oliver Gondža
Oliver Gondža

Reputation: 3511

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.

Upvotes: 1

Related Questions