How to Solve Error Message Cannot Find Symbol ApplicationContext in Java Spring Application

Posted on

Introduction

This is an article where the main focus is about how to solve an error message which is appear upon compiling, building or running a Java Spring Application. The error appear as ‘Cannot Find Symbol ApplicationContext’. It has a relation with the previous article. That article is an article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’. The article exist in this link for further reference. The focus of that article is to show how to create a simple Java Spring Application.

But apparently, upon creating a main Java Spring Application file, there will be an error message appear. The following is the content of the Java Spring Application file with the name of ‘SpringMain.java’ :

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

Actually, upon compiling, building or running that class where the main method exist, an error message will appear. It does not recognize the ‘ApplicationContext’ class name. Actually, the ‘ApplicationContext’ class name is very important to load the Spring application configuration file. The Spring application configuration file in this example exist in ‘src/main/resources’.

Solution to Solve Error Message Cannot Find Symbol ApplicationContext

The solution for handling the error message is very simple. It is because there is no ApplicationContext class available in the application. So, import the ApplicationContext class in order to solve the problem. Importing the class is very easy if there is another jar file available containing that class file. In this example, the process for importing the class is using the pom.xml project build descriptor. It is a file which is useful for maven as a project build descriptor. The following are the step to solve it :

  1. Open the file with the name of ‘pom.xml’ in the NetBeans IDE for an example. Actually, the original content of the pom.xml file as an example below is the project build descriptor of the simple Java Application in the other article. Furthermore, that article is an article exist in this link with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’. The following is the actual content of the 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>com.test</groupId>
        <artifactId>spring</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    </project>
    
  2. Next, just add the following package dependencies’ definition :

    <dependencies>
         <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>5.3.9</version>
         </dependency>
    </dependencies>
    

    Overall, after joining all, the final configuration will be exist as follow :

    <?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>com.test</groupId>
        <artifactId>spring</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <dependencies>
        <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>5.3.9</version>
        </dependency>
        </dependencies>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    </project>
    

The definition above will help the project or application to identify the missing ApplicationContext class file. Upon the compile, build and run process, it will download the spring-context.jar file which is important for importing the ApplicationContext class file. The reason is that the ApplicationContext class file exist in the spring-context.jar file. So, after defining it in pom.xml file following the success for downloading it from the maven central repository, the error will no longer appear.

Leave a Reply