eclipse Servlet 中的休眠导致 NoClassDefFoundError: org/slf4j/LoggerFactory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1689659/
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
Hibernate in Servlet causesNoClassDefFoundError: org/slf4j/LoggerFactory
提问by jon-hanson
I have a Java (6) application which uses Hibernate (V3.3.2) to read data from HSQLDB, which i build and debug/run with Eclipse (V3.5.1), and it works fine.
我有一个 Java (6) 应用程序,它使用 Hibernate (V3.3.2) 从 HSQLDB 读取数据,我使用 Eclipse (V3.5.1) 构建和调试/运行它,并且它工作正常。
I then created a GWT (V1.7) Servlet web app, copied my hibernate classes into it, and added the same User Libraries dependencies. However when i run the servlet and try to access a URL which invokes my code i get this:
然后,我创建了一个 GWT (V1.7) Servlet Web 应用程序,将我的休眠类复制到其中,并添加了相同的用户库依赖项。但是,当我运行 servlet 并尝试访问调用我的代码的 URL 时,我得到以下信息:
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
at xxx.daoimpl.DAOSession.initialise(DAOSession.java:40)
where DAOSession.java:40 is :
其中 DAOSession.java:40 是:
AnnotationConfiguration config = new AnnotationConfiguration ();
Googling for this error suggests i am missing slf4j-api.jar from the classpath, however if i look at the command line for the Debug properties i can see this jar there:
谷歌搜索此错误表明我在类路径中缺少 slf4j-api.jar,但是如果我查看 Debug 属性的命令行,我可以在那里看到这个 jar:
C:\java\jsedk_6\jre\bin\javaw.exe
-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:54541
-Xmx512m -Dfile.encoding=Cp1252
-classpath
D:\dev\workspace\xxx\src;
D:\dev\workspace\xxx\resources;
D:\dev\workspace\xxx\war\WEB-INF\classes;
C:\java\eclipse\plugins\com.google.gwt.eclipse.sdkbundle.win32_1.7.1.v200909221731\gwt-windows-1.7.1\gwt-user.jar;
C:\java\eclipse\plugins\com.google.gwt.eclipse.sdkbundle.win32_1.7.1.v200909221731\gwt-windows-1.7.1\gwt-dev-windows.jar;
C:\java\hibernate-annotations-3.4.0.GA\hibernate-annotations.jar;
C:\java\hibernate-annotations-3.4.0.GA\lib\ejb3-persistence.jar;
C:\java\hibernate-annotations-3.4.0.GA\lib\hibernate-commons-annotations.jar;
C:\java\hibernate-distribution-3.3.2.GA\hibernate3.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\antlr-2.7.6.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\commons-collections-3.1.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\dom4j-1.6.1.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\javassist-3.9.0.GA.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\jta-1.1.jar;
C:\java\hibernate-validator-4.0.1.GA\hibernate-validator-4.0.1.GA.jar;
C:\java\hibernate-validator-4.0.1.GA\lib\validation-api-1.0.0.GA.jar;
C:\java\hibernate-validator-4.0.1.GA\lib\log4j-1.2.14.jar;
C:\java\hsqldb\lib\hsqldb.jar;
C:\java\restlet-jse-2.0m5\lib\org.restlet.jar;
C:\java\restlet-jee-2.0m5\lib\org.restlet.ext.servlet.jar;
C:\java\restlet-jse-2.0m5\lib\org.restlet.ext.xml.jar;
C:\java\slf4j-1.5.8\slf4j-api-1.5.8.jar;
C:\java\slf4j-1.5.8\slf4j-log4j12-1.5.8.jar
com.google.gwt.dev.HostedMode
...
If i open the jar i can see the LoggerFactory class in there.
如果我打开罐子,我可以在那里看到 LoggerFactory 类。
Any idea why it isn't being found by the class loader?
知道为什么类加载器找不到它吗?
Edit 1:If try to access org.slf4j.LoggerFactory from my code, Eclipse compiles it ok, but i get the same error at runtime.
编辑 1:如果尝试从我的代码访问 org.slf4j.LoggerFactory,Eclipse 可以编译它,但我在运行时遇到相同的错误。
Edit 2:If i add a Test class with a main which calls the same code and run it, it works. So this classpath problem seems specific to the Servlet.
编辑 2:如果我添加一个带有调用相同代码并运行它的 main 的测试类,它就可以工作。所以这个类路径问题似乎是 Servlet 特有的。
thanks, jon
谢谢,乔恩
回答by jon-hanson
It seems that copying the two slf4j jars to the war/WEB-INF/lib sub-project/dir fixed the problem. I'm not really sure why i should need to do this for these two jars and not for all the other Hibernate, Restlet etc jars that the project also uses, though i suppose for consistency i will do that anyway - i guess it will make deployment easier as well.
似乎将两个 slf4j jar 复制到 war/WEB-INF/lib 子项目/dir 解决了问题。我不太确定为什么我需要为这两个 jar 执行此操作,而不是为项目也使用的所有其他 Hibernate、Restlet 等 jar 执行此操作,尽管我想为了一致性我还是会这样做 - 我想它会部署也更容易。
If someone can provide some sort of explanation as to why this worked and why exactly i need to do it i will select it as the "correct" answer, otherwise i'll select this one.
如果有人可以提供某种解释为什么这行得通以及为什么我需要这样做,我会选择它作为“正确”答案,否则我会选择这个。
回答by mhaller
Can you please reconfirm that you have at least two slf4j.jar files in the classpath, the slf4j-api.jar and exactly one implementation, such as slf4j-jdk14.jar?
您能否再次确认类路径中至少有两个 slf4j.jar 文件,即 slf4j-api.jar 和一个实现,例如 slf4j-jdk14.jar?
You must not have multiple sflj4-implementations in the classpath.
类路径中不得有多个 sflj4 实现。
回答by non sequitor
You have a runtime dependency issue so everything compiles fine but the dependency is in your jars. You need to visit the Hibernate site and look at the compatibility matrixand make sure you are matched up correctly then check the dependencies for annotations and core. You log4J jars look fine so it's definitely some quirk.
您有运行时依赖问题,因此一切都可以正常编译,但依赖关系在您的 jar 中。您需要访问 Hibernate 站点并查看兼容性矩阵并确保您正确匹配,然后检查注释和核心的依赖项。你的 log4J jars 看起来不错,所以这绝对是一些怪癖。
If it works in Eclipse then logically it's definitely some diff between the 2 runtimes (eclipse and non-eclipse), if the matrix checks out fine then see if you can factor out the commonalities in the 2 then work out the differences -- your answer should be in there.
如果它在 Eclipse 中工作,那么从逻辑上讲,它在 2 个运行时(日食和非日食)之间肯定存在一些差异,如果矩阵检查正常,然后看看您是否可以找出 2 中的共性,然后找出差异 - 您的答案应该在里面
回答by Brian Deterling
I had a similar issue except I was using Tomcat and the NoClassDefFound error was on the juli logger. I fixed it by removing the Tomcat dependencies from my classpath when running in hosted mode because hosted mode embeds a Tomcat server that was conflicting. So I would see what happens if you remove some or all of the restlet jars from your classpath to see if they conflict.
我有一个类似的问题,除了我使用的是 Tomcat 并且 NoClassDefFound 错误出现在 juli 记录器上。我通过在托管模式下运行时从我的类路径中删除 Tomcat 依赖项来修复它,因为托管模式嵌入了一个冲突的 Tomcat 服务器。因此,如果您从类路径中删除部分或全部 restlet jar 以查看它们是否发生冲突,我会看看会发生什么。