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.