简体   繁体   中英

Eclipse + Maven + Spring Application: java.lang.NoClassDefFoundError

There are many stackoverflow questions around this subject but, they surprisingly don't seem to address my situation.

I have validated the following things:

  1. No conflicting Spring libraries. I am using the latest releases in Maven repository
  2. I am using Maven plugin for eclipse and running it in eclipse using run class as Java Application. No assembly needed
  3. I have manually checked the jar file in my local Maven repository. It has EnvironmentCapable.class in the path it is expected
  4. It is a simple application which is basically code from Chapter 1 of Spring in Action book

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.springinaction</groupId>
  <artifactId>knights</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>knights</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>4.4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
  </dependencies>
</project>

The main class code that does not run.

package com.springinaction.knights;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

public class KnightMain {

    public static void main(String[] args) throws Exception {

        AbstractApplicationContext context = new AnnotationConfigApplicationContext(KnightConfig.class);

        Knight knight = (Knight)context.getBean(Knight.class);
        knight.embarkOnQuest();
        context.close();
    }
}

Gist of error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)

If you got in your local repository some old artifacts you wish to get rid of, just do this:

  1. On command line go to folder where your pom.xml is located
  2. type and execute the next command: dependency:purge-local-repository

That command will clean your local repository, from dependencies that are being used in your project.

Look at the description from Maven website:

在此处输入图片说明

Now, to that:

  1. On command line go to folder where your pom.xml is located
  2. type and execute the next command: dependency:copy-dependencies

Look at the description from Maven website:

在此处输入图片说明

This command will pull from remote repository the necessary dependencies to your local repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM