繁体   English   中英

java.lang.NoClassDefFoundError: org/springframework/core/io/Resource 异常

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

我对 Spring 和 maven 都很陌生。 我收到一个错误。 我正在从命令行使用 Maven 来构建项目。

我已经更新了 spring-core 和 spring-context 依赖项,之后我完成了mvn clean install并且它说没问题。 我做了mvn eclipse:eclipse之后它也很好。 我用mvn clean compile ,也通过了。 我已经检查了 .m2 文件夹(在 MacOS 上),是的 spring-core 在那里。 然而在运行我的项目时,这个运行时异常即将到来; 我已经尝试了 Stack Overflow 上给出的其他几种解决方案,但都没有奏效。

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>

我这边有一个快速的疑问,这可以 ( beans -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)......

应用程序.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();



    }
}

雇员.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);
}


}

spring-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 插件)时的相同项目正在运行。

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

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

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

请尝试以下操作:

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

发布是 spring-web 只能在 spring.core 4.2 版本之后工作

暂无
暂无

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

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