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

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

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

javaspringmavenapplicationcontext

提问by Adnan

I am new to spring and maven. I have a simple hello world project using Spring . The project builds successfully but i face the following error at runtime:

我是 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

This is the main app: App.java

这是主应用程序: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();
    }
}

This is the HelloWorld bean

这是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);
    }
}

This is the annotation based configuration file:

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

package com.test.SpringMaven2;


import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig{

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

}

This is my pom.xml

这是我的 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>

I am running the following command in CMD to run the application: java -cp {nameofresultjarfile} com.test.SpringMaven2.App

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

But getting the error messsage above

但是得到上面的错误信息

Any advice?

有什么建议吗?

Thanks

谢谢

采纳答案by jaysee

Your pom.xmlcreates a jar file which contains only the classes defined in your project. But all the other dependencies (spring-core, spring-context-support) which are required by your application aren't included.

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

If your application is started within eclipse, eclipse integrates these required jar files to the classpath of the application and so it is able to run.
If your application is started from CMD the spring classes can't be found in the classpath and you get a java.lang.NoClassDefFoundError.

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

It's possible to name all the required jars manually when the application is started from CMD (classpath), but is much easier to created a so called self contained jar file, which has all of them already included. This can be done using maven-assembly-plugin.

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

An example how to create a self contained jar file can be found here.

可以在此处找到如何创建自包含 jar 文件的示例。

The application in a self contained jar can be started now from CMD:
java -jar name_of_your_project-jar-with-dependencies.jar

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

回答by Abhishek Galoda

I was getting the same error when running from Intellij as a main class (In Spring Boot project)

从 Intellij 作为主类运行时,我遇到了同样的错误(在 Spring Boot 项目中)

After opening Module settings, In the Deopendencies tab I saw that for spring-context for some reason the scope was provided, changing to compile/ even removing the scope fixed the issue for for me

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

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