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

14,342

Solution 1

You are trying to run whole application using only .class file and forgetting to tell JVM about dependencies.

Following issue can be solved using: mvn exec:java -Dexec.mainClass="Spring_1.App" However I strongly recommend to build jar file or execute it from your IDE (Eclipse, Idea)

Solution 2

I think that by running your app in that way the classpath does not contain all the dependencies.

Try the following:

  1. Build the project by using: mvn clean package (this should create a jar file in your target/ folder)
  2. Run your app with: jara -jar your_app_name.jar
Share:
14,342
Rishi
Author by

Rishi

Updated on July 10, 2022

Comments

  • Rishi
    Rishi almost 2 years

    I am very new to both Spring and maven. I am getting an error. I am using Maven from command line to build the project.

    I have updated spring-core and spring-context dependencies, after that I have done mvn clean install and it says no problem. I did mvn eclipse:eclipse after it and it also went fine. I compile with mvn clean compile and that passes too. I have checked in .m2 folder ( on MacOS) that yes spring-core is there. Yet while running my project, this run time exception is coming; I have tried several other solutions given on Stack Overflow but none of them worked.

    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
    

    This is my POM file.

    <?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>
    

    One quick doubt from my side, could this ( 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">
    

    be the problem-root??

    Edit 1 Exact run command which is causing exception to occur

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

    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>
    

    Same project when imported to eclipse (with m2e plugin) is working.

  • Rishi
    Rishi about 6 years
    jara is java (assuming this); also tried with it yet the same error. The other answer helped.
  • Rishi
    Rishi about 6 years
    awesome man!!! Thanks a ton :) ; Quick question why it is recommended to run jar with IDE and why java -jar your_app_name.jar does not work?
  • Kucera.Jan.CZ
    Kucera.Jan.CZ about 6 years
    IDE helps you with all the boilerplate, java -jar does not work because of the same reason as running java .class -> it's jar without external dependencies. If you still needed I would recommend to check how to build fat jars
  • Rishi
    Rishi about 6 years
    fat jars!! that name ringed a bell in my mind :D Thank you for all the help :) may you have a great day today.