83

When I run my project, I get numerous outputs of this error:

Sep 9, 2009 8:22:23 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Jersey threw exception
java.lang.NoClassDefFoundError: Could not initialize class SpringFactory
        at com.point2.prospect.persistence.hibernate.HibernateTransactionInterceptor.doFilter(HibernateTrans
actionInterceptor.java:17)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at com.point2.prospect.restapi.ServerErrorInterceptor.doFilter(ServerErrorInterceptor.java:27)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:619)

I notice that this problem has a pattern of being asked all over the web, but with no real answers. What is a general cause for this kind of error?

Michael Wiles
  • 20,902
  • 18
  • 71
  • 101

7 Answers7

218

NoClassDefFound error is a nebulous error and is often hiding a more serious issue. It is not the same as ClassNotFoundException (which is thrown when the class is just plain not there).

NoClassDefFound may indicate the class is not there, as the javadocs indicate, but it is typically thrown when, after the classloader has loaded the bytes for the class and calls "defineClass" on them. Also carefully check your full stack trace for other clues or possible "cause" Exceptions (though your particular backtrace shows none).

The first place to look when you get a NoClassDefFoundError is in the static bits of your class i.e. any initialization that takes place during the defining of the class. If this fails it will throw a NoClassDefFoundError - it's supposed to throw an ExceptionInInitializerError and indicate the details of the problem but in my experience, these are rare. It will only do the ExceptionInInitializerError the first time it tries to define the class, after that it will just throw NoClassDefFound. So look at earlier logs.

I would thus suggest looking at the code in that HibernateTransactionInterceptor line and seeing what it is requiring. It seems that it is unable to define the class SpringFactory. So maybe check the initialization code in that class, that might help. If you can debug it, stop it at the last line above (17) and debug into so you can try find the exact line that is causing the exception. Also check higher up in the log, if you very lucky there might be an ExceptionInInitializerError.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Michael Wiles
  • 20,902
  • 18
  • 71
  • 101
  • 23
    Thank you. Turning my attention to static initializer blocks saved hours of aggravation. – Boris Brudnoy Oct 06 '11 at 21:23
  • 2
    "i.e. any initilisation that takes place during the defining of the class" -- Thank you! – Matt MacLean Jan 06 '12 at 16:22
  • 2
    Another good idea, is to pipe your log to a file and look for java.lang.ClassNotFoundException. There might be a stray one of those there, which will tell you what class it was trying to load that produced all the NoClassDefFoundError exceptions. Worked for me. – Ryan Shillington Aug 23 '13 at 18:08
  • And something to add... I have also found you get a NoClassDefFoundError if a class does not find a class it needs. So you'll get a class not found exception for the class it can't find and a NoClassDefFoundError for the class being defined. – Michael Wiles Sep 17 '13 at 09:39
  • Worked for me, in my situation, it was a class that tried to read configuration parameters from a file, and the application instance had to be constructed with the full path to that file. – Ashesh Aug 06 '15 at 11:02
  • Thank you for saying to look for an earlier ExceptionInInitializerError, that contained the exact reason why it wasn't working, all the following NoClassDefFoundError didn't contain anything worthwhile. – qwertzguy Jan 17 '17 at 23:23
  • Perfect response. In CDI 1.1. with the WELD version bundled in weblogic, the way CDI creates proxies for beans is different. namely, WELD is inspecting way too much to build the proxy. ANd it ends up getting illegal access errors while trying to create the proxies, because the proxy creation logic is attempting to explore private enums or classes defined in the proxied class. After that class def error forever without a proper indication of why the def error. The first exception is crucial to get the access error. – 99Sono Jan 20 '17 at 17:00
  • 1
    Thanks, this helped resolve a sneaky . Not sure _why_ `It will only do the ExceptionInInitializerError the first time it tries to define the class, after that it will just throw NoClassDefFound` though. If it's a problem w/ the static initializer, shouldn't that be the exception _every_ time? – Tianxiang Xiong Mar 14 '18 at 00:00
  • 1
    The reason for that is that the static initaliser is only run once... that rule is kept consistently. So if it runs it once and it fails, it throws the ExceptionInInitializer error. After that it caches the result of the static initaliser... – Michael Wiles Mar 15 '18 at 22:19
6

You're missing the necessary class definition; typically caused by required JAR not being in classpath.

From J2SE API:

public class NoClassDefFoundError extends LinkageError

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

skaffman
  • 398,947
  • 96
  • 818
  • 769
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
1

I had faced the same issue, because the jar library was copied by other Linux user(root), and the logged in user(process) did not have sufficient privilege to read the jar file content.

Kayvan Tehrani
  • 3,070
  • 2
  • 32
  • 46
1

I had this:

 class Util {
  static boolean isNeverAsync = System.getenv().get("asyncc_exclude_redundancy").equals("yes");
}

you can probably see the problem, the env var might return null instead of string. So just to test my theory, I changed it to:

 class Util {
  static boolean isNeverAsync = false;
}

and the problem went away. Too bad that Java can't give you the exact stack trace of the error though, kinda weird.

0

I recently ran into this error on Windows 10. It turned out that windows was looking for .dll files necessary for my project and couldn't find them because it looks for them in the system path, PATH, rather than the CLASSPATH or -Djava.library.path

0

I had this same Error and it turns out, I had a method that was being called over and over, like an infinite loop, except it wasn't in an actual loop.

Hef
  • 606
  • 6
  • 16
-1

Realised that I was using OpenJDK when I saw this error. Fixed it once I installed the Oracle JDK instead.

allkenang
  • 1,511
  • 15
  • 12