How to Solve the Generation Error "Invalid method Code length"

by Michela Frigerio
5,309 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

You can get the "java.lang.ClassFormatError: Invalid method Code length" error when generating the layout of your Web Project. If you see this error, it means that you used a template in the Web Project that is too large to be compiled. This often happens in cell templates, when the template handles the rendering of a large number of units.

To solve the problem, you have to break down the template into smaller units. At the end of this process you will have one main template (unit, cell, and so on) and different "helper" templates, which are called from the main one. Here are some guidelines:

  • Locate a piece of code that can be placed in an independent file.
  • Create a "helper" template in the same directory of the main template which will contain this piece of code.
  • Place the portion of code into the helper template.
  • Declare all the Groovy variables that the helper template needs to get successfully compiled.
  • In the main template, declare the Groovy variable that represents the directory in which the main template is placed. If the error is not related to a cell template, you only have to change the following Groovy variables in order to get the layout of the element to which the template is related, and the correspondent directory.
   def cellLayout = getCellLayout(cell)

   def templateDir = getElementLayoutFile(cellLayout.path, ".cell.template")
                     .parentFile

   def templatePath = templateDir.absolutePath + 
                      "/helperTemplateName.Helper.template"

   def templateParams= null

  • In the main template, remove the piece of code copied into the helper template and write the following code that calls the helper template.
   <wr:PrintTemplate var="templateParams" template="templatePath"/>

 
 

Related Learning Objects