简体   繁体   English

Eclipse + Maven + Spring应用程序:java.lang.NoClassDefFoundError

[英]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. 关于这个问题有很多stackoverflow问题,但是令人惊讶的是,它们似乎并没有解决我的情况。

I have validated the following things: 我已经验证了以下内容:

  1. No conflicting Spring libraries. 没有冲突的Spring库。 I am using the latest releases in Maven repository 我正在使用Maven存储库中的最新版本
  2. I am using Maven plugin for eclipse and running it in eclipse using run class as Java Application. 我正在使用Maven Eclipse插件,并使用Run类作为Java Application在Eclipse中运行它。 No assembly needed 无需组装
  3. I have manually checked the jar file in my local Maven repository. 我已经在本地Maven存储库中手动检查了jar文件。 It has EnvironmentCapable.class in the path it is expected 它在预期的路径中具有EnvironmentCapable.class
  4. It is a simple application which is basically code from Chapter 1 of Spring in Action book 这是一个简单的应用程序,基本上是Spring in Action手册第1章中的代码

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.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 在命令行上转到pom.xml所在的文件夹
  2. type and execute the next command: dependency:purge-local-repository 输入并执行下一个命令: 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: 查看来自Maven网站的描述:

在此处输入图片说明

Now, to that: 现在,到:

  1. On command line go to folder where your pom.xml is located 在命令行上转到pom.xml所在的文件夹
  2. type and execute the next command: dependency:copy-dependencies 输入并执行下一个命令: dependency:copy-dependencies

Look at the description from Maven website: 查看来自Maven网站的描述:

在此处输入图片说明

This command will pull from remote repository the necessary dependencies to your local repository. 该命令将从远程存储库中将必要的依赖项拉至本地存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的Spring和Maven应用程序-java.lang.NoClassDefFoundError异常 - Simple Spring and Maven Application - java.lang.NoClassDefFoundError Exception java.lang.NoClassDefFoundError:在eclipse maven中 - java.lang.NoClassDefFoundError: in eclipse maven Maven Spring WebApp上的java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError on maven spring webapp Maven的java.lang.noclassdeffounderror - java.lang.noclassdeffounderror for Maven Eclipse Maven使用外部Jar测试java.lang.NoClassDefFoundError - Eclipse Maven Test java.lang.NoClassDefFoundError with External Jar Eclipse,Maven项目上的线程“ main”中的异常java.lang.NoClassDefFoundError - Exception in thread “main” java.lang.NoClassDefFoundError on Eclipse, Maven project 使用Maven Eclipse AsciidoctorJ时出现java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError while using maven eclipse AsciidoctorJ 在Eclipse中使用Maven创建可执行jar-java.lang.NoClassDefFoundError - Create executable jar with maven in eclipse - java.lang.NoClassDefFoundError 春季java.lang.NoClassDefFoundError - Spring java.lang.NoClassDefFoundError Maven Eclipse Spring JUNIT java.lang.NoClassDefFoundError:com.filenet.apiimpl.util.J2EEUtil(初始化失败) - Maven Eclipse Spring JUNIT java.lang.NoClassDefFoundError: com.filenet.apiimpl.util.J2EEUtil (initialization failure)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM