9 Replies Latest reply: May 18, 2013 10:02 PM by andy dufresne RSS

    Troubleshooting NoClassDefFoundError: Could not initialize class

    andy dufresne
      I am looking for guidelines for troubleshooting NoClassDefFoundError where class initialization fails. The stacktrace displayed when such an error occurs does not contain the original error.

      A sample trace is as below
      org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
           at org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:370)
           at org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:330)
           at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:322)
           at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:167)
           at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:172)
           at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:143)
           at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:118)
           at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:79)
           at com.spmsoftware.dataexportprocessing.service.excel.ExcelDataExporter.export(ExcelDataExporter.java:58)
           at com.spmsoftware.dataexportprocessing.service.DataExportProcessRunner.run(DataExportProcessRunner.java:175)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
           at java.lang.reflect.Method.invoke(Method.java:601)
           at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
           at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
           at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
           at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
           at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
           at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
           at $Proxy371.run(Unknown Source)
           at com.spmsoftware.worker.taskprocessing.flow.stage.ExecutionStage.run(ExecutionStage.java:128)
           at com.spmsoftware.worker.taskprocessing.flow.stage.ExecutionStage.expand(ExecutionStage.java:90)
           at com.spmsoftware.worker.taskprocessing.flow.stage.ExecutionStage.followExecutionFlow(ExecutionStage.java:64)
           at com.spmsoftware.worker.taskprocessing.flow.stage.ExecutionStage.enterStage(ExecutionStage.java:44)
           at com.spmsoftware.worker.taskprocessing.flow.FlowHelper.setCurrentStage(FlowHelper.java:58)
           at com.spmsoftware.worker.taskprocessing.flow.stage.PreValidationStage.goToExecution(PreValidationStage.java:44)
           at com.spmsoftware.worker.taskprocessing.flow.stage.PreValidationStage.enterStage(PreValidationStage.java:33)
           at com.spmsoftware.worker.taskprocessing.flow.FlowHelper.setCurrentStage(FlowHelper.java:58)
           at com.spmsoftware.worker.taskprocessing.flow.FlowHelper.process(FlowHelper.java:53)
           at com.spmsoftware.worker.taskprocessing.ProcessingSlot.processTask(ProcessingSlot.java:90)
           at com.spmsoftware.worker.taskprocessing.ProcessingSlot.doProcess(ProcessingSlot.java:74)
           at com.spmsoftware.worker.taskprocessing.ProcessingSlot.run(ProcessingSlot.java:58)
      Caused by: org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
           at org.apache.poi.xssf.usermodel.XSSFFactory.newDocumentPart(XSSFFactory.java:73)
           at org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:355)
           ... 32 more
      Caused by: java.lang.reflect.InvocationTargetException
           at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
           at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
           at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
           at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
           at org.apache.poi.xssf.usermodel.XSSFFactory.newDocumentPart(XSSFFactory.java:71)
           ... 33 more
      Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType
           at org.apache.poi.xssf.model.StylesTable.createDefaultFills(StylesTable.java:487)
           at org.apache.poi.xssf.model.StylesTable.initialize(StylesTable.java:452)
           at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:87)
           ... 38 more
      },date=2013-02-28T03:10:12.372-08:00}
      |#]
      Any guidelines on how can we troubleshoot such NoClassDefFoundError? Note I am trying to get an answer for this specific error trace.
        • 1. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
          Kayaman
          andy dufresne wrote:
          Any guidelines on how can we troubleshoot such NoClassDefFoundError?
          It means the class definition is not found by the classloader. This could mean that a library is missing or it is in the wrong place (where the classloader can't find it).
          Note I am trying to get an answer for this specific error trace.
          Find out where that specific class is supposed to be.
          • 2. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
            andy dufresne
            The error does not mean that the class definition is not found. Most of the times this error is displayed when static method/variable initialization fails but the error trace does not indicate the right issue
            • 3. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
              gimbal2
              Generally how i would do it:

              1. read the manual of whatever api you're using to know which external dependencies it has which must also be on the classpath
              2. failing that, look which class cannot be found, copy/paste its name into google and figure out which jar you need that way
              3. failing that, copy/paste the exception message ("org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException") into google and see if you can come up with something. Usually you are not the first to run into it
              4. in case of jar hell, meaning you have library conflicts, you'll have to resolve the conflict by removing whatever duplicate jar is unwanted (usually an older version) or make sure the newest jar is loaded first

              Jar hell can occur more easily in web application environments, where the server itself might have an older version of a library you need which causes conflicts. if this is the case you'll have to read in the manual or ask in communities of the specific product you're using how to properly deal with that. Usually deploying the proper library version with your application should be enough, but I can't speak for all products out there.

              EDIT:

              a source code search (hidden option #5 if I'm really desperate) indicates the failing line is this:
              86          // Initialization required in order to make the document readable by MSExcel
              87             initialize();
              Not much to go on there either, but the problem may be relating to the library that the StyleSheetDocument class is in - it is not part of POI itself.
              import  org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
              • 4. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
                Kayaman
                andy dufresne wrote:
                The error does not mean that the class definition is not found.
                Excuse me? That's exactly what it means.

                Do you have the ooxml-schemas-1.0.jar library?

                Edited by: Kayaman on Feb 28, 2013 4:21 PM
                • 5. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
                  andy dufresne
                  Do you have the ooxml-schemas-1.0.jar library?
                  Yes I do. That's the first thing I verified. Also since the error says "could not initialize class" it gives an indication that there is some problem in initialization.

                  I am trying to figure out ways of troubleshooting such scenarios where we do not have the source code to add try catch block in order to get hold of the actual exception.
                  • 6. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
                    andy dufresne
                    Generally how i would do it:

                    1. read the manual of whatever api you're using to know which external dependencies it has which must also be on the classpath
                    2. failing that, look which class cannot be found, copy/paste its name into google and figure out which jar you need that way
                    3. failing that, copy/paste the exception message ("org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException") into google and see if you can come up with something. Usually you are not the first to run into it
                    4. in case of jar hell, meaning you have library conflicts, you'll have to resolve the conflict by removing whatever duplicate jar is unwanted (usually an older version) or make sure the newest jar is loaded first
                    I did already follow all the above steps to see if the class file is missing but that isn't the case. Neither there is a jar hell.

                    >
                    EDIT:

                    a source code search (hidden option #5 if I'm really desperate) indicates the failing line is this:
                    86          // Initialization required in order to make the document readable by MSExcel
                    87             initialize();
                    I didn't understand why do you mention the initialize() method call made in the StylesTable constructor. As per the stack trace the error occurs in StylesTable class line number 487. This line has the below code (after decompiling)
                            ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
                    It seems that the classloader failed in initializing STPatternType class when it encountered this line. This error could be a failure in initializing static methods or static variables (I suspect the SchemaType variable). To understand the root cause I guess the suggested approach is to modify the source code and add try catch blocks. But that isn't something I could do since I do not have the source code.

                    This post is to understand what do others do in such cases?
                    Not much to go on there either, but the problem may be relating to the library that the StyleSheetDocument class is in - it is not part of POI itself.
                    import  org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
                    I didn't understand why do refer to the StyleSheetDocument class.
                    • 7. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
                      Kayaman
                      andy dufresne wrote:
                      Yes I do. That's the first thing I verified.
                      Good.

                      And EJP has a point. But you could still run it through a debugger or even decompile the code, if you can't find the actual source.

                      Edited by: Kayaman on Mar 1, 2013 11:03 AM
                      • 8. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
                        EJP
                        I should add here that the documentation for NoClassDefFoundError doesn't distinguish it adequately from ClassNotFoundException, with which it is often therefore confused. I can say with certainty that one reason it is thrown is if the .class file is renamed or moved around the directory hierarchy such that the package/class it contains no longer agrees with its entire file name.
                        • 9. Re: Troubleshooting NoClassDefFoundError: Could not initialize class
                          andy dufresne
                          Apparently this error occurs for different third party libraries on each occurance and we verified that the class file is not renamed or moved. The class for which the error occurs has static fields and static blocks which would be getting executed and probably throwing an error which is not getting captured.
                          What could be reason why an error in the static initialization block or while initializing a static variable does not get captured in the error trace?