当前位置: 首页 > 知识库问答 >
问题:

ClassNotFoundException错误,即使jar文件存在于WEB-INF/lib目录中

皇甫飞飙
2023-03-14

我正在尝试设置tomcat服务器,使所有常见的spring jar都进入tomcat lib目录,因为我有很多web应用程序使用spring。然而,我的一个web应用程序也使用了带有quartaz jar的spring batch。当所有JAR都位于app1/web-INF/lib目录中时,这个web应用程序工作正常。然而,当我试图将所有常见的spring jar移动到tomcat/lib目录以及app1/WEB\u INF/lib中的spring batch和quartaz jar时,我发现了noclassdefFound错误。这是堆栈跟踪

Caused by: java.lang.NoClassDefFoundError: org/quartz/ObjectAlreadyExistsException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    at java.lang.Class.getDeclaredConstructors(Class.java:1836)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:230)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:972)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:945)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:777)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:707)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:523)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:357)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:335)
    at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:187)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:895)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 60 more
Caused by: java.lang.ClassNotFoundException: org.quartz.ObjectAlreadyExistsException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

但是当我将同一个jar复制到tomcat/lib目录时,它可以完美地工作。至于我的理解。类文件搜索的顺序是首先jdk-

**EDIT**

这里是pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kp</groupId>
    <artifactId>kp-app1</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>kp app1 Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>com.kp.parent</groupId>
        <artifactId>kp-common</artifactId>
        <version>1.0.0.0</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cxf.version>2.7.5</cxf.version>
        <spring.version>3.2.3.RELEASE</spring.version>
        <aspectj.version>1.7.3</aspectj.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>3.0-alpha-1</servlet.version>
        <spring.batch.version>2.2.1.RELEASE</spring.batch.version>
        <quartz.version>1.8.5</quartz.version>
        <skipTests>true</skipTests>
    </properties>

    <dependencies>


        <!-- Spring framework -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.0.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- ================== -->
        <!-- AOP -->
        <!-- ================== -->

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- ================== -->
        <!-- SPRING BATCH -->
        <!-- ================== -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-infrastructure</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>


        <!-- Required for cache/ lazy loading -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- Quartz framework -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>




    <build>

        <finalName>kp-app1</finalName>
        <outputDirectory>${project.basedir}/target/classes</outputDirectory>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>

        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[2.8,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>

            <!-- Tomcat 6 plugin -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat6-maven-plugin</artifactId>
                <configuration>
                    <url>${serverUrl}:${port}/manager</url>
                    <path>/${contextpath}</path>
                    <username>${username}</username>
                    <password>${password}</password>
                </configuration>
            </plugin>

            <!-- Maven compiler -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp/WEB-INF/</directory>
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>web.xml</include>
                            </includes>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

            <!--  Copy all the provided jars-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <outputDirectory>${project.build.directory}/provided-jars</outputDirectory>
                    <transitive>true</transitive>
                    <includeScope>provided</includeScope>
                </configuration>
                <executions>
                    <execution>
                        <id>cop-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Create a zip of all the provided jars -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/resources/assembly/zip.xml</descriptor>
                    </descriptors>
                    <finalName>nms-tomcat-jars</finalName>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- append to the packaging phase. -->
                        <goals>
                            <goal>single</goal> <!-- goals == mojos -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

共有2个答案

邢凌
2023-03-14

由于应用程序上下文加载类。当我移动Spring-context.jar时,Spring-web.jarspring-context-support.jar.它开始工作。正如@Naveen Ramawat所建议的那样。上下文加载器不会从web-inf/class目录(子节点)加载类。因此,当我将这些jars移动到web-inf/lib子类时,能够从tomcat lib目录加载父类。

谷梁涵忍
2023-03-14

子类加载器可以找到父类加载器中加载的类,但父类加载器无法找到子类加载器加载的类。这里,spring类需要quartz类来创建bean,但失败了,因为quartz类是由子类加载器加载的。

 类似资料:
  • 问题内容: 我正在使用Eclipse,我需要能够将Java库(JAR文件)添加到Web应用程序的文件夹中。我该如何实现? 问题答案: 将jar文件添加到您的WEB-INF / lib文件夹中。 在Eclipse中右键单击您的项目,然后转到“构建路径>配置构建路径” 添加“ Web应用程序库”库 这将确保所有WEB-INF / lib jar都包含在类路径中。

  • 根据这个问题,我创建了一个实现Catalina LifecycleListener类的类。 如何在Tomcat启动时运行脚本? 当我将JAR文件放置在webapps/root/web-inf/lib中时,Tomcat启动失败,出现类未找到的异常: java.lang.ClassNotFoundException:Net.Company.Tomcat.StartupEventHookListener

  • Web应用程序的WEB-Inf/lib目录是用于jar文件的,servlet容器(例如Tomcat)会自动将这些文件带入应用程序的类路径。对于Java9的模块化jar,这种机制如何工作(或者将来应该如何工作)有什么想法吗?这些提供了模块,用户必须在其中指定应用程序应该使用哪些包含/导出的模块/包。(旧的)类路径还有一个模块路径。 模块化的jar是否可以在没有任何进一步规范/配置的情况下工作,以便所

  • 我在webapp/WEB-INF/lib/文件夹下添加了一个依赖项。 当我使用maven构建时,我希望在war中将它复制到WEB-INF/lib,而不是WEB-INF/classes/WEB-INF/lib。我正在寻找这是使用maven本身来完成的。 我已经在pom文件中提到了打包作为战争,但是依赖项没有被复制。我是不是在pom文件里漏掉了什么。我已经复制了下面的pom: 附上战争内容截图。

  • 我是新手。与Gradle我有多个项目构建文件。大多数项目构建文件都经过了适当的配置,工作良好,并生成了正确的输出。我正在尝试将其中一个项目从ANT build转换为Gradle build。我能够编译Java文件,并且能够生成WAR归档。 我在生成的WAR文件中注意到的一件事是,WEB-INF/lib目录中包含了额外的JAR文件。当我从ANT build system构建项目时,只有很少的jar文

  • 我正在创建一个(Minecraft)Forge mod,它使用(非官方的)Discord API JDA。我在EclipseIDE中这样做。 在IDE中,我可以添加带有依赖项的JDA,并且代码中没有错误。然后,使用gradlew并编译它,当我尝试运行它时(在Minecraft中)会出现一个错误。 我让几个人检查了我的身材。gradle要确保它是正确的,而且是正确的。我现在假设这是一个一般的Java