java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven

java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven

我是 spring 和 maven 的新手。我有一个使用 Spring 的简单 hello world 项目。项目构建成功,但我在 运行 时遇到以下错误:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

这是主应用程序:App.java

package com.test.SpringMaven2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;



/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        System.out.println( "Hello World!" );
        ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);

        HelloWorld hello = (HelloWorld) context.getBean("helloWorld"); 
        hello.setName("Adnan");
        hello.getName();
    }
}

这是 HelloWorld bean

package com.test.SpringMaven2;

public class HelloWorld{


    private String name; 

    public void setName(String name){
        this.name = name;
    }

    public void getName(){
        System.out.println("Hello, " + name);
    }
}

这是基于注解的配置文件:

package com.test.SpringMaven2;


import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig{

    @Bean
    public HelloWorld helloWorld(){
        return new HelloWorld(); 
    }

}

这是我的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test</groupId>
  <artifactId>SpringMaven2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringMaven2</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>4.3.3.RELEASE</version>
</dependency>
  </dependencies>
</project>

我正在 运行 在 CMD 中向 运行 应用程序执行以下命令: java -cp {nameofresultjarfile} com.test.SpringMaven2.App

但是得到上面的错误信息

有什么建议吗?

谢谢

您的 pom.xml 创建了一个 jar 文件,其中仅包含项目中定义的 类。但是不包括您的应用程序所需的所有其他依赖项(spring-core,spring-context-support)。

如果您的应用程序是在 eclipse 中启动的,eclipse 会将这些必需的 jar 文件集成到应用程序的类路径中,因此它能够 运行.
如果您的应用程序是从 CMD 启动的,则无法在类路径中找到 spring 类,您会得到一个 java.lang.NoClassDefFoundError.

当应用程序从 CMD(类路径)启动时,可以手动命名所有必需的 jar,但创建一个所谓的自包含 jar 文件要容易得多,该文件已包含所有这些文件。这可以使用 maven-assembly-plugin.

来完成

可以找到如何创建独立 jar 文件的示例 here

独立 jar 中的应用程序现在可以从 CMD 启动:
java -jar name_of_your_project-jar-with-dependencies.jar

当 运行 来自 Intellij 作为主要 class(在 Spring 引导项目中)时,我遇到了同样的错误

打开模块设置后,在 Deopendencies 选项卡中我看到 spring-context 出于某种原因提供了范围,更改为编译/甚至删除范围为我解决了问题

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>## Put your desired version here ##</version>   
    </dependency>

在我的类似情况下,@jaysee 都回答或:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.example.MyMainClass </mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
</plugin>

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.0</version>

有帮助。

下面为我解决了类似的问题。

构建插件部分应该放在 /dependencies 之后:

<build>
  <plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.4</version>
            <configuration>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                  <goals>
                    <goal>repackage</goal>
                  </goals>
                </execution>
            </executions>
    </plugin>
  </plugins>
</build>

我遇到了同样的问题。我使用的是 intellij 2021,但上述解决方案无效。同样在日食中它工作正常。对我有用的解决方案是: 1) 去编辑 run/debug 配置 2)修改选项 3)选中“包含提供范围的依赖项”