Error java.lang.NoClassDefFoundError on org.springframework.webflow.util.RandomGuid

20,686

Solution 1

It is important to keep two or three different exceptions strait in our head in this case:

  1. java.lang.ClassNotFoundException This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.

  2. java.lang.NoClassDefFoundError This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason - now we're trying again, but we're not even going to try to load it, because we failed loading it earlier. The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

That being said, another answer poster indicates that the RandomGUID requires a call to InetAddress.getLocalHost(). On many operating systems, this would trigger a host lookup that would use the hosts file (/etc/hosts on *NIX systems, %WINDOWS%/system32/drivers/etc/HOSTS on a Windows system.)

I have seen similar errors quite frequently when that file incorrectly defines the localhost address. 127.0.0.1 should point to 'localhost' (and probably also localhost.localdomain.) It should NOT point to the actual host name of the machine (although for some reason, many older RedHat Linux installers liked to set it incorrectly.)

Solution 2

NoClassDef: The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found. As you're missing a spring class I'd guess that you're missing one of the spring jar files.

There are 2 places to put jars in tomcat, there's a global area (which in 5 is something like common/lib and is a bit different in tomcat6) and the area only for your webapp, which is webapps/mywebapp/WEB-INF/lib. The jars for your app should really go here, but if you're really perplexed and can't figure out any other way to make it work they'll work in the global (if you have only some of them in the global it might also break, as they might need a class in another jar and if one piece is in the local it won't work. This is especially true for spring libraries because there's a number of separate jars, not just one).

your CLASSPATH is a list of everywhere the jvm looks for classes. This could include directories of class files, or jar or zip files of classes, which are listed like directories. Tomcat should load that for you by using the above mentioned directories correctly.

Solution 3

Nowadays, the environment variable $CLASSPATH should not be used; instead, the java application should have the classpath set on the command line.

However, in the case of tomcat and libraries used in the webapps, you simply put the JARs (for Spring) into the shared/lib/ folder of the tomcat installation.

Solution 4

The reason is failure to load class RandomGUID.

From looking at RandomGUID source, most of the chances that its static initializer failed at InetAddress.getLocalHost().

Do you have some strange networking config on the host? For example, no or weird localhost definition in /etc/hosts ?

Share:
20,686
serhads
Author by

serhads

I am a french developper, using a lot of differents tools. I use eXtreme Programming and agile good practises, and I love tools that help me to save time.

Updated on March 25, 2020

Comments

  • serhads
    serhads about 4 years

    I am sorry, my question is stupid, but I am not able to answer it, as a java illiterate. I run a tomcat (5) on CentOS5 (for a CAS server), and when I try to open this URL http://192.168.1.17:8080/cas-server-webapp-3.3.1/login I get this error :

    first error: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.webflow.util.RandomGuid

    and root error: org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.webflow.util.RandomGuid

    $CLASSPATH is empty, and it seems to be a problem, but I don't know what to put in it.

    EDIT: Jared is right, my hosts files defined 127.0.0.1 as localhost, and now it work very well!

  • matt b
    matt b over 15 years
    if the static initializer failed, the JVM would throw a ExceptionInInitializerError, not a NoClassDefFoundError.
  • Yoni Roit
    Yoni Roit over 15 years
    No, not really. I've seen 1000 times how "java.lang.NoClassDefFoundError: Could not initialize class" is thrown when static class initializer fails. But yeah, it's probably just me :-)
  • Jared
    Jared over 15 years
    The JVM would likely throw an ExceptionInInitializeError the FIRST time the class load fails, but would likely throw a NoClassDefFoundError on subsequent load attempts (it's smart enough not to retry loading the class if it failed before.)
  • Jared
    Jared over 15 years
    I've had similar problems before (especially on older RedHat Linux releases) where the /etc/hosts file was hosed (no entry for 127.0.0.1, or 127.0.0.1 is aliased to the actual host name rather than localhost.)