解决war包部署后找不到本地jar包的问题(ClassNotFoundException、NoClassDefFoundError)

大多数Web项目都会引用一些本地jar包,然后最终以war包的形式部署上线。但实际运行时jar包里面的类根本找不到,出现各种ClassNotFoundException、NoClassDefFoundError等错误。

这里记录一下注意事项和正确的解决方法:

1、先简单说下怎么配置war包部署(如果你原来是jar包部署的话):

<project ...>
	<packaging>war</packaging>
    ...
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        ...
    </dependencies>
</project>

如上,pom.xml文件中加入packaging标签,写war;然后加入tomcat的provided级别的依赖。然后在IDEA中Build Artifacts即可生成war包,默认在项目根目录的target文件夹中。

2、假设我们引入了一个本地jar包(json-simple-1.1.1.jar),我这边文件放在 resources/lib 目录下,你也可以自定义其他目录,然后继续配置pom文件,在 dependencies 标签下加子标签:

<!-- jar libs -->
<dependency>    
 	<groupId>lib1</groupId>    
 	<artifactId>lib1</artifactId>    
  	<scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/json-simple-1.1.1.jar</systemPath>
</dependency>

groupId和artifactId都可以瞎写,无所谓的,但scope和systemPath必须正确。

3、最关键的一步,因为要让本地依赖的jar包生效必须让它们置于 webapps/xxx/WEB-INF/lib 目录下才可以,所以继续配置pom文件:

<project ...>
	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${project.basedir}/src/main/resources/lib</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

然后再打包就可以了。

发布了91 篇原创文章 · 获赞 204 · 访问量 54万+
展开阅读全文

项目打成war部署到tomcat,本地没问题部署到服务器就报这个错,怎么解决

10-23

23-Oct-2018 16:06:36.750 SEVERE [localhost-startStop-25] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.NoSuchMethodError: org.springframework.expression.spel.SpelParserConfiguration.<init> (Lorg/springframework/expression/spel/SpelCompilerMode;Ljava/lang/ClassLoader;)V at org.springframework.context.expression.StandardBeanExpressionResolver.<init>(StandardBeanExpressionResolver.java:98) at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:557) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4821) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5264) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:939) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1812) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 23-Oct-2018 16:06:36.753 INFO [localhost-startStop-25] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext 问答

Tomcat部署war后,运行时出现如下错误

01-03

一月 03, 2017 8:38:02 下午 org.apache.catalina.core.ContainerBase addChildInternal 严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/save]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1092) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1984) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 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:745) Caused by: java.lang.NoClassDefFoundError: Lweka/classifiers/Classifier; at java.lang.Class.getDeclaredFields0(Native Method) at java.lang.Class.privateGetDeclaredFields(Class.java:2583) at java.lang.Class.getDeclaredFields(Class.java:1916) at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106) at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:270) at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:139) at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:65) at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:417) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:891) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:388) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) ... 10 more Caused by: java.lang.ClassNotFoundException: weka.classifiers.Classifier at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735) ... 24 more 一月 03, 2017 8:38:02 下午 org.apache.catalina.startup.HostConfig deployWAR 严重: Error deploying web application archive E:\apache-tomcat-7.0.73-windows-x64\apache-tomcat-7.0.73\webapps\save.war java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/save]] at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:903) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1092) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1984) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 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:745) 有没有人能告诉我这是哪里出了问题 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览