java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven [英] java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven

查看:293
本文介绍了java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

This is the main app: 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

This is the 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(); 
    }

}

这是我的 pom.xml

This is my 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

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

但是得到上面的错误信息

But getting the error messsage above

有什么建议吗?

谢谢

推荐答案

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

Your pom.xml creates 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.

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

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.

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

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.

可以找到如何创建自包含 jar 文件的示例 这里.

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

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

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

这篇关于java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆