java.lang.NoClassDefFoundError:org / springframework / core / io / Resource Exception

时间:2018-04-25 15:26:17

标签: java spring maven spring-mvc maven-2

我对Spring和maven都很新。我收到这个错误,我会告诉我到目前为止做了什么

我从命令行使用Maven来构建项目。

我已经更新了spring-core和spring-context依赖项,之后我已经完成了mvn clean install并且没有问题。我做了mvn eclipse:eclipse之后它也很好。我使用mvn clean编译并且也通过了。我已经检查了.m2文件夹(在MacOS上),是的spring-core就在那里。然而,在运行我的项目时,这个运行时异常即将来临;我已经尝试过在StackOverflow上给出的其他几个解决方案但没有一个能够工作。 *请不要downvote;我想要非常诚实地学习一些东西;自从过去13个小时以来一直在编码。

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/Resource

        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
    Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.Resource
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 7 more

这是我的POM文件。

<?xml version="1.0" encoding="UTF-8"?>

<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>Spring_1</groupId>
  <artifactId>SpringCIList</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>SpringCIList</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.6.RELEASE</version>
    </dependency>



   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

从我这边快速怀疑,这可能是(豆-2.5 xsd)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

是问题根源?

编辑1导致异常发生的精确运行命令

.

Rishis-MBP:SpringCIList rishiprakash$ cd target/classes/
Rishis-MBP:classes rishiprakash$ ls
Spring_1        spring-module.xml
Rishis-MBP:classes rishiprakash$ java Spring_1.App
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
    at java.lang.Class.getDeclaredMethods0(Native Method)......

App.java

package Spring_1;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class App
{

    public static void main( String[] args )
    {

Resource r = new ClassPathResource("spring-module.xml");
BeanFactory factory = new XmlBeanFactory(r);
Employee e = (Employee)factory.getBean("emp");
        System.out.println( "Hello World!" );
        e.displayInfo();



    }
}

Employee.java

package Spring_1;

class Employee{

private  String name;

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

void displayInfo(){
  System.out.println("name of employee is"+this.name);
}


}

弹簧module.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="emp" class="Spring_1.Employee">
<property name="name" value="Rishi"></property>
</bean>


  </beans>

导入到eclipse(使用m2e插件)时的相同项目正在运行。

3 个答案:

答案 0 :(得分:2)

您正在尝试仅使用.class文件运行整个应用程序,而忘记告诉JVM有关依赖项。

以下问题可以使用以下方法解决:mvn exec:java -Dexec.mainClass="Spring_1.App" 但是我强烈建议您从IDE(Eclipse,Idea)

构建jar文件或执行它

答案 1 :(得分:1)

我认为通过以这种方式运行您的应用程序,类路径不包含所有依赖项。

尝试以下方法:

  1. 使用以下代码构建项目:mvn clean package(这应该在目标/文件夹中创建一个jar文件)
  2. 使用以下代码运行您的应用:jara -jar your_app_name.jar

答案 2 :(得分:0)

Release is spring-web仅在spring.core 4.2版之后可用