java NoClassDefFoundError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5282509/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:24:06  来源:igfitidea点击:

NoClassDefFoundError

javahibernatejetty

提问by sweety

I am working with the hibernate and at the time of using hibernate Connection i am getting Error as below

我正在使用休眠,在使用休眠连接时,我收到如下错误

java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.Configuration
at com.ensarm.niidle.server.SchemaManager.getDatabaseSession(SchemaManager.java:60)
at com.ensarm.niidle.server.helper.UserServiceHelper.createNiidleUser(UserServiceHelper.java:27)
at com.ensarm.niidle.server.UserSignUpServlet.doPost(UserSignUpServlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

Please help me in solving it i have added all the required hibernate jar files with the lib folder of my Web App.

请帮我解决这个问题,我已经在我的 Web 应用程序的 lib 文件夹中添加了所有必需的休眠 jar 文件。

now what should i do?

现在我该怎么办?

回答by Luke Woodward

I suspect you're missing a log4j jar or a commons-logging.jar.

我怀疑您缺少 log4j jar 或 commons-logging.jar。

The message Could not initialize class org.hibernate.cfg.Configurationmeans that the JVM has already tried and failed to initialize the class org.hibernate.cfg.Configuration. The source code of the classcontains the following line:

该消息Could not initialize class org.hibernate.cfg.Configuration表示 JVM 已尝试初始化该类但失败org.hibernate.cfg.Configuration该类源代码包含以下行:

private static Log log = LogFactory.getLog( Configuration.class );

Since this field is marked static, the field's value is set when the class has initialized. This field is the only static member of this class, and so if the JVM has failed to initialize this class, there must have been a problem with this field.

由于该字段被标记为static,该字段的值在类初始化时设置。这个字段是这个类唯一的静态成员,所以如果JVM初始化这个类失败,那么这个字段肯定有问题。

回答by Arjan Tijms

java.lang.NoClassDefFoundErroris a tricky exception. It doesn't necessarily mean the class is not there. Instead, it's thrown to indicate there has been some problem when first initializing the class.

java.lang.NoClassDefFoundError是一个棘手的例外。这并不一定意味着该类不存在。相反,它被抛出以表明在第一次初始化类时出现了一些问题。

This is particularly troublesome when the very first access to this class runs into this problem (and the root problem is reported here), but subsequent accesses to the class only spit out the dreaded java.lang.NoClassDefFoundErrorwithout any reference to the original problem.

当第一次访问这个类时遇到这个问题(并且这里报告了根本问题)时,这尤其麻烦,但随后访问该类只吐出可怕的,java.lang.NoClassDefFoundError而没有参考原始问题。

In this case, your configuration might contain errors or is maybe missing altogether?

在这种情况下,您的配置可能包含错误或可能完全丢失?

回答by luis.espinal

What arjan said. The error you are having doesn't say that it cannot find cfg.Configuration. It simply says that somewhere in the initialization of cfg.Configuration, a class definition was not found. Were there other exceptions in your log?

阿让说什么。您遇到的错误并不是说它找不到 cfg.Configuration。它只是说在 cfg.Configuration 初始化的某个地方,没有找到类定义。您的日志中是否还有其他异常?

This smells to me classes cfg.Configuration depends on are missing from your app's WEB-INF/lib directory.

这让我觉得你的应用程序的 WEB-INF/lib 目录中缺少 cfg.Configuration 依赖的类。

Take a look at this thread in the Spring Forums, they seem to talk about the problem you are having:

看看 Spring Forums 中的这个帖子,他们似乎在谈论您遇到的问题:

http://forum.springsource.org/showthread.php?t=65296

http://forum.springsource.org/showthread.php?t=65296

In particular see this post made by one of the senior members where he mentions four additional jars that were missing (mind you, this was in 2008, and I have no clue what hibernate and spring versions you are using):

特别是看到一位高级成员发表的这篇文章,他提到了另外四个丢失的 jar(请注意,这是在 2008 年,我不知道您使用的是什么 hibernate 和 spring 版本):

http://forum.springsource.org/showpost.php?s=f3b5de4e9df29e65d4c9acaafc1676c8&p=219678&postcount=10

http://forum.springsource.org/showpost.php?s=f3b5de4e9df29e65d4c9a​​caafc1676c8&p=219678&postcount=10

Hope it helps.

希望能帮助到你。

回答by AdrianRM

I was just having a very similar exception message using Hibernate 3.5.4 ('java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.AnnotationConfiguration') and it was related to logging, as Luke Woodward pointed out. So I would guess that you are missing a slf4j implementation with its corresponding dependencies.

正如 Luke Woodward 指出的那样,我在使用 Hibernate 3.5.4('java.lang.NoClassDefFoundError:无法初始化类 org.hibernate.cfg.AnnotationConfiguration')时遇到了一个非常相似的异常消息,它与日志记录有关。所以我猜你缺少一个带有相应依赖项的 slf4j 实现。

The link that luis.espinal provided ( http://forum.springsource.org/showpost.php?s=f3b5de4e9df29e65d4c9acaafc1676c8&p=219678&postcount=10) points to possible JARs that you could be missing. Among them is slf4j-log4j12-1.5.0.jar .

luis.espinal 提供的链接(http://forum.springsource.org/showpost.php?s=f3b5de4e9df29e65d4c9a​​caafc1676c8&p=219678&postcount=10)指向可能丢失的 JAR。其中包括 slf4j-log4j12-1.5.0.jar 。

On top of this, the pom.xml in the 'First Hibernate Application' tutorial in the online hibernate documentation contains an explicit reference to a slf4j implementation:

最重要的是,在线休眠文档的“第一个休眠应用程序”教程中的 pom.xml 包含对 slf4j 实现的显式引用:

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/tutorial.html#tutorial-firstapp-setup

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/tutorial.html#tutorial-firstapp-setup

I was using the Ant Maven Tasks and my problem was solved adding the dependency to slf4j-log4j12.

我正在使用 Ant Maven 任务,我的问题解决了将依赖项添加到 slf4j-log4j12。