0

EDIT: This question's top answer talks about different compilation and production environments. The second answer is about fixing it in Eclipse. I am running this code in IntelliJ and I don't have different environments for compilation and production. I am just hitting the Run button in IntelliJ. Also the question is nowhere a duplicate of what is asked there.

I am trying to build a simple Spring program. This is the folder structure:

.
├── firstSpringProject.iml
├── pom.xml
├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── webograffiti
│       │           ├── MainClass.java
│       │           └── Student.java
│       └── resources
│           └── applicationContext.xml

Here is the code written in these files:

Student.java

package com.webograffiti;

public class Student {

    private String name;

    public String getName() {
        return name;
    }

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

    public void displayInfo(){
        System.out.println("Hello "+name);
    }
}

MainClass.java

package com.webograffiti;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainClass {
    public static void main(String [] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student obj = (Student) context.getBean("student");
        obj.displayInfo();
    }
}

applicationContext.xml

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

    <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-3.0.xsd">

    <bean id = "student" class = "com.webograffiti.Student">
        <property name = "name" value = "Piyush"/>
    </bean>

</beans>

I am trying to run MainClass.main(). But getting this exception:

Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.beans.support.ResourceEditorRegistrar.<init>(Lorg/springframework/core/io/ResourceLoader;)V
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:446)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:355)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.webograffiti.MainClass.main(MainClass.java:8)

I have all the dependencies needed to run this code, viz. spring-core, spring-beans, spring-osgi-core. This could be a classpath error but I am not able to figure out a solution.

Piyush Shrivastava
  • 1,046
  • 2
  • 16
  • 43
  • 1
    *java.lang.NoSuchMethodError* indicates incompatible versions of jar files. – Jens May 25 '18 at 05:33
  • This is 99% of the time due to different versions of libraries between build and deployment environments (and gets asked repeatedly here). Please double check that you have exactly the same libraries in both places. – Jim Garrison May 25 '18 at 05:33
  • @JimGarrison I don't have separate environments, and I am running it in IntelliJ. Can you help me fixing this? I can't get a solution from the question you think this is a duplicate of. – Piyush Shrivastava May 25 '18 at 05:41
  • Are you deploying to an embedded server within IntelliJ? if yes, check the server's configuration and classpath. If not, you should probably do a completely clean rebuild of your entire project to refresh all the .class files. – Jim Garrison May 25 '18 at 05:44
  • @JimGarrison, The MainClass file has a main(). The generated Jar file is running on my local system. It is not running on a server. Also, I did a `mvn clean install`, the problem still persists. – Piyush Shrivastava May 25 '18 at 05:46
  • It may be time to reinstall IntelliJ. The only way this error happens is if you compile against a dependency that has that particular constructor `ResourceEditorRegistrar(org.springframework.core.io.ResourceLoader loader)` but when IntelliJ tried to execute the code, it successfully loaded the class `ResourceEditorRegistrar` but could not find that particular constructor. Is it possible you have two different versions of the SAME library in your classpath? – Jim Garrison May 25 '18 at 05:51
  • @JimGarrison, Please reopen this question so that someone who actually has an idea of what's going on can answer. – Piyush Shrivastava May 25 '18 at 05:52
  • I will but there's no guarantee someone else won't close it again. – Jim Garrison May 25 '18 at 05:56

0 Answers0