Thursday, April 9, 2009

NoClassDefFoundError in JSP for Class with static block on tomcat restart (spuriously)

The other day I had a strange problem with a Spring/JSF application.
Every now and then when I restarted tomcat, my application has thrown a NoClassDefFoundError in a JSP.
The reason for this is quite complex.
The JSP tried to execute a static method of a session bean.
In this session bean, I had a static block which was loaded from a Spring bean class.

As the session gets restored some times before the ApplicationContext was created during startup, this leaded to a NullPointer exception. (This was seen in the tomcat log, but you will notice it later in the jsp).

Example:

public class MySessionClass implements Serializable {
// the static block which causes the problem later during session restore
// as the SpringBeanClass is not available during session restore in tomcat startup.
public final static String myStaticField = SpringBeanClass.staticMethod();

public static void myMethod(){...}
}


Solution:
I resolved it in the getter Method of the field, where I initialize the field if not set and therefore, I get rid of the static field.

No comments:

Post a Comment

Due to the high amount of Spam, you must solve a word verification.