'package org.springframework.beans.factory does not exist error
I try to implement a simple hello world program using Spring framework. I have tried the following example in Windows environment without using IDE. In Eclipse IDE, it works fine.
In Windows command line prompt, I get following error messages.
C:\Users\Vijay\Documents\javaex\ex1>javac -d . Test.java
Test.java:3: error: package org.springframework.beans.factory does not exist
import org.springframework.beans.factory.BeanFactory;
^
Test.java:4: error: package org.springframework.beans.factory.xml does not exist
import org.springframework.beans.factory.xml.XmlBeanFactory;
^
Test.java:5: error: package org.springframework.core.io does not exist
import org.springframework.core.io.ClassPathResource;
Test.java:6: error: package org.springframework.core.io does not exist
import org.springframework.core.io.Resource;
^
Test.java:10: error: cannot find symbol
Resource resource=new ClassPathResource("applicationContext.xml");
^
symbol: class Resource
location: class Test
Test.java:10: error: cannot find symbol
Resource resource=new ClassPathResource("applicationContext.xml");
^
symbol: class ClassPathResource
location: class Test
Test.java:11: error: cannot find symbol
BeanFactory factory=new XmlBeanFactory(resource);
^
symbol: class BeanFactory
location: class Test
Test.java:11: error: cannot find symbol
BeanFactory factory=new XmlBeanFactory(resource);
^
symbol: class XmlBeanFactory
location: class Test
Please find the folder contents.
C:\Users\Vijay\Documents\javaex\ex1>dir
26-04-2022 16:19 472 applicationContext.xml
26-04-2022 19:22 <DIR> com
15-06-2012 17:30 61,464 com.springsource.org.apache.commons.logging-1.1.1.jar
15-06-2012 17:30 547,837 org.springframework.beans-3.0.1.RELEASE-A.jar
15-06-2012 17:30 355,598 org.springframework.core-3.0.1.RELEASE-A.jar
26-04-2022 17:16 250 Student.java
26-04-2022 17:16 519 Test.java
C:\Users\Vijay\Documents\javaex\ex1>echo %JAVA_HOME%
C:\Program Files\Java\jdk-18.0.1\bin
C:\Users\Vijay\Documents\javaex\ex1>echo %CLASSPATH%
C:\Users\Vijay\Documents\javaex\ex1
Student.java
package com.vijay;
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);
}
}
Test.java
package com.vijay;
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 Test {
public static void main(String[] args) {
Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
Student student=(Student)factory.getBean("studentbean");
student.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="studentbean" class="com.vijay.Student">
<property name="name" value="Hello Vijay"></property>
</bean>
</beans>
I have put Spring core jar files in same folder.
JAVA_HOME and CLASS PATH looks fine. I couldn't figure out why import is failing.
It would be great if someone helps to find out what could have been wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|