Java “NoClassDefFoundError:无法初始化类”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1401111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"NoClassDefFoundError: Could not initialize class" error
提问by
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?
我注意到这个问题有一种在整个网络上被问到的模式,但没有真正的答案。这种错误的一般原因是什么?
回答by ChssPly76
You're missing the necessary class definition; typically caused by required JAR not being in classpath.
您缺少必要的类定义;通常由所需的 JAR 不在类路径中引起。
From J2SE API:
从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.
公共类 NoClassDefFoundError 扩展了 LinkageError
如果 Java 虚拟机或 ClassLoader 实例尝试加载类的定义(作为正常方法调用的一部分或作为使用 new 表达式创建新实例的一部分)并且找不到类的定义,则抛出。
编译当前正在执行的类时,搜索到的类定义存在,但无法再找到该定义。
回答by Michael Wiles
NoClassDefFound error is a nebulous error and is often hiding a more serious issue. It is notthe same as ClassNotFoundException (which is thrown when the class is just plain not there).
NoClassDefFound 错误是一个模糊的错误,通常隐藏着一个更严重的问题。它是不一样的ClassNotFoundException(被抛出时,该类只是普通的不存在)。
NoClassDefFound mayindicate 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).
NoClassDefFound可能表明该类不存在,正如 javadocs 所示,但通常在类加载器加载类的字节并对其调用“defineClass”之后抛出。还要仔细检查您的完整堆栈跟踪是否有其他线索或可能的“原因”异常(尽管您的特定回溯没有显示)。
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.
当您获得 NoClassDefFoundError 时,首先要查看的是类的静态位,即在类定义期间发生的任何初始化。如果失败,它将抛出 NoClassDefFoundError - 它应该抛出 ExceptionInInitializerError 并指出问题的详细信息,但根据我的经验,这些很少见。它只会在第一次尝试定义类时执行 ExceptionInInitializerError,之后它只会抛出 NoClassDefFound。所以看看之前的日志。
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.
因此,我建议查看该 HibernateTransactionInterceptor 行中的代码并查看它需要什么。似乎无法定义类SpringFactory。所以也许检查该类中的初始化代码,这可能会有所帮助。如果您可以调试它,请将其停在上面 (17) 的最后一行并调试,以便您可以尝试找到导致异常的确切行。还要检查日志中的更高层,如果您很幸运,可能会出现 ExceptionInInitializerError。
回答by Kayvan Tehrani
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.
我遇到了同样的问题,因为 jar 库被其他 Linux 用户(root)复制,并且登录用户(进程)没有足够的权限来读取 jar 文件内容。
回答by allkenang
Realised that I was using OpenJDK when I saw this error. Fixed it once I installed the Oracle JDK instead.
当我看到这个错误时意识到我正在使用 OpenJDK。一旦我安装了 Oracle JDK,就修复了它。
回答by allkenang
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:
您可能会看到问题所在,环境变量可能会返回 null 而不是字符串。所以只是为了测试我的理论,我把它改成了:
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.
问题就解决了。太糟糕了,Java 无法为您提供错误的确切堆栈跟踪,这有点奇怪。