将 jar 标记为提供时出现 java.lang.NoClassDefFoundError

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

java.lang.NoClassDefFoundError when marking a jar as provided

javamaventomcatwarpackaging

提问by Denden Oussama

I have the follwing dependency in my webapp:

我的 webapp 中有以下依赖项:

<!-- swagger -->
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-springmvc</artifactId>
    <version>${version.swagger.springmvc}</version>
</dependency>

I want to reduce the size of the war file so I marked the swagger jar as provided:

我想减小 war 文件的大小,所以我将 swagger jar 标记为提供的:

<!-- swagger -->
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-springmvc</artifactId>
    <version>${version.swagger.springmvc}</version>
    <scope>provided</scope>
</dependency>

I added the swagger jar to tomcat's common lib folder so it should be provided by tomcat during runtime, but when i try to deply the app I got this exception:

我将 swagger jar 添加到 tomcat 的公共 lib 文件夹中,因此它应该在运行时由 tomcat 提供,但是当我尝试部署应用程序时,我得到了这个异常:

java.lang.IllegalStateException: Cannot load configuration class: com.mangofactory.swagger.configuration.DocumentationConfig
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:368)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:244)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:633)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1279)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1192)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getDeclaredMethods(Class.java:1962)
at org.springframework.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:348)
at org.springframework.cglib.proxy.Enhancer.getMethods(Enhancer.java:421)
at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:456)
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:118)
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:92)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:358)
... 23 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
... 36 more

I notice that the problem is comming from the org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses but i have all the spring jar required declared in my pom without the provided scope also when running the webapp without the provided scope it work fine any idea?

我注意到问题来自 org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses 但我在没有提供范围的情况下在我的 pom 中声明了所有需要的弹簧罐?

采纳答案by Melou

I think the issue comes from another missing jar. In the stacktrace we see

我认为问题来自另一个丢失的罐子。在堆栈跟踪中我们看到

Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver

The com.fasterxml.classmate.TypeResolver is present in the jar classmate.jar, which is a dependency from swagger-springmvc.

com.fasterxml.classmate.TypeResolver 存在于 jar classmate.jar 中,它是 swagger-springmvc 的依赖项。

When you use scope compile, the dependencies of swagger-springmvc are deployed as well. With scope provided, you have to provide the swagger-springmvc jar, but also all its dependencies. Here, you need to provided classmate.jar.

使用范围编译时,也会部署 swagger-springmvc 的依赖项。提供范围后,您必须提供 swagger-springmvc jar,以及它的所有依赖项。在这里,您需要提供 classmate.jar。