How to Solve Error Message java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist

Posted on

Introduction

It is an error where the error appear upon compiling, building or running the Java Spring Application in the NetBeans IDE. It has a relation with another article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link. Actually, the file with the name of ‘spring-config.xml’ is important in order to run a Java Spring Application. Actually, the file name can be vary and it is flexible depend on the environment.

But the most important thing is that the ‘spring-config.xml’ file is very useful for defining the Spring Bean information. The name of the file is flexible as long as it is an XML file. For an example, in this context the name is ‘spring-config.xml’. The ApplicationContext class will use that information to load and instantiate all of the Spring Bean defined in the Spring configuration file.  As soon as the execution of the Java Spring Application starts, it generate the following error message :

Caused by: java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist

Actually the following is the actual content of the spring-config.xml file :

<?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="employeeBean" class="com.test.spring.Employee">
    </bean>
</beans>

Solution

Solving the problem to handle error message is very easy. Apparently, the error message above appear because of the Java Spring Application cannot find the ‘spring-config.xml’ file. Furthermore, since it cannot find the file, it cannot load and instantiate any Spring Bean defined in that file. The following is the content of the main class of the Java Spring Application where the ApplicationContext class try to find and load the file :

public class SpringMain {
       public static void main(String[] args){
              ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
              Employee employeeObject = (Employee) context.getBean("employeeBean");
              employeeObject.identifyID();
       }
}

As in the above snippet code of the main class from the Java Spring Application, there is one important line. That line is in the following focus :

ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

That line trigger an error file not found. So, the problem is the file exist but the main class of the Java Spring Application cannot find and load it. So, the solution in this context is actually to move that file. Currently, in the example, the file exist in folder ‘src’. After searching online and using Google search engine, there is actually an exact location to place the file. The file must exist in ‘src/java/resources’. So, the steps for solving the problem exist below :

  1. Just create the folder ‘resources’ if it does not exist in ‘src/java’. In this context, it is using NetBeans IDE. Just right click at the main folder of the Java Spring Application and select the menu for creating a new folder. It will trigger the appearance of the following image display :

    How to Solve Error Message java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist
    How to Solve Error Message java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist

    The above process for creating the folder exist in NetBeans IDE. The folder with the name of ‘resources’ is important to store the Java Spring Application file configuration. Just click the Finish button to create the new folder.

  2. After clicking the Finish button, the folder with the name ‘resources’ will appear as in the following image :

    How to Solve Error Message java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist
    How to Solve Error Message java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist
  3. Finally, run the program once more. If there are no other errors appear, the Java Spring Application will run properly. It exist as in the following image :

    How to Solve Error Message java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist
    How to Solve Error Message java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist

Leave a Reply