【问题标题】:Keep getting java.lang.NoClassDefFoundError in Spring在 Spring 中不断收到 java.lang.NoClassDefFoundError
【发布时间】:2017-06-24 23:47:21
【问题描述】:

我真的不知道我的 Spring 配置有什么问题。你能帮我一把吗?

我的代码(简化版):

App.java:

public class App {
public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
    ctx.close();
}

}

Config.java:

@Configuration
@ComponentScan
public class Config {
    @Bean
    public JdbcOperations jdbcTemplate(DataSource ds) {
        return new JdbcTemplate(ds);
    }
}

pom 文件:

 <properties>

    <!-- Generic properties -->
    <java.version>1.6</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Spring -->
    <spring-framework.version>4.2.5.RELEASE</spring-framework.version>

    <!-- Hibernate / JPA -->
    <hibernate.version>4.2.1.Final</hibernate.version>

    <!-- Logging -->
    <logback.version>1.0.13</logback.version>
    <slf4j.version>1.7.5</slf4j.version>

    <!-- Test -->
    <junit.version>4.11</junit.version>

</properties>

<dependencies>
    <!-- Spring and Transactions -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Logging with SLF4J & LogBack -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
        <scope>runtime</scope>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>


    <!-- Test Artifacts -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring-framework.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.185</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

我的文件夹结构:

maven 打包后,运行 jar 得到:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/annotation/AnnotationConfigApplicationContext
        at App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.annotation.AnnotationConfigApplicationContext
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

其中,第 10 行是AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);

此外: 我在看春季教程时正在写代码:Spring Framework Tutorial 4: Database programming with JdbcTemplate

他的源代码在:github 和源码唯一不同的是我在pom文件中添加了MainClass。

非常感谢!
真诚的

【问题讨论】:

    标签: java spring maven configuration annotations


    【解决方案1】:

    您也可以使用 Shade 运行它来构建带有依赖项的 jar。

    只需在目标中添加“shade:shade”即可。

    示例: mvn clean install shade:shade

    【讨论】:

      【解决方案2】:

      这样做的原因是,在你的 jar 中你只有你的类,没有任何依赖。要在 jar 中包含所有依赖项,您需要创建一个 fat jar。

      用 pom.xml 中的以下内容替换您的构建块:

          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-assembly-plugin</artifactId>
                      <version>2.4.1</version>
                      <configuration>
                          <!-- get all project dependencies -->
                          <descriptorRefs>
                              <descriptorRef>jar-with-dependencies</descriptorRef>
                          </descriptorRefs>
                          <!-- MainClass in mainfest make a executable jar -->
                          <archive>
                              <manifest>
                                  <mainClass>App</mainClass>
                              </manifest>
                          </archive>
      
                      </configuration>
                      <executions>
                          <execution>
                              <id>make-assembly</id>
                              <!-- bind to the packaging phase -->
                              <phase>package</phase>
                              <goals>
                                  <goal>single</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      

      运行 mvn clean package 后,你会在你的目标目录中得到两个 jar,第一个是没有依赖关系的,第二个是有依赖关系的。运行第二个 jar。

      1. CustomerJdbc-0.0.1-SNAPSHOT.jar
      2. CustomerJdbc-0.0.1-SNAPSHOT-jar-with-dependencies.jar

      【讨论】:

        【解决方案3】:

        你得到它的原因是因为 maven-jar-plugin 实际上并不包含任何默认情况下在 POM 文件中引用的 JAR。

        有几种方法可以解决这个问题,this answer 似乎很全面。

        另请注意,在 POM 中引用主类时,需要使用完全限定的类名 (net.ubilife.spring.customerjdbc.App) 来引用它。

        【讨论】:

        • 感谢您的回复!但是该应用程序位于根文件夹中。我刚刚上传了我的问题中的文件夹结构。
        猜你喜欢
        • 2021-08-15
        • 1970-01-01
        • 2018-09-16
        • 2021-10-19
        • 1970-01-01
        • 1970-01-01
        • 2020-12-02
        • 2022-11-02
        • 2018-12-03
        相关资源
        最近更新 更多