cannot access org.springframework.context.ConfigurableApplicationContext class file – Java

Photo of author
Written By M Ibrahim
intellij-idea maven spring-boot

Quick Fix: To resolve the issue, follow these steps:

  1. Select "File" > "Invalidate Cache and Restart".
  2. Go to "File" > "New" > "Project from existing source".
  3. Select only the "pom.xml" file (not the entire project directory) to load the project.

Spring will automatically set up the project.

The Problem:

Unable to access the "org.springframework.context.ConfigurableApplicationContext" class. The "cannot access org.springframework.context.ConfigurableApplicationContext class file" error appears while running a Spring Boot project, indicating that the necessary JAR file containing this class is missing.

The Solutions:

Solution 1: Checking Maven Dependencies and IDE Configuration

To resolve this error, follow these steps:

  1. Check the POM File:
    • Review the dependencies in your pom.xml file to ensure that the required Spring Boot dependencies are included and their versions are compatible.
  2. Invalidate Cache and Restart IDE:
    • Go to the File menu in your IDE (IntelliJ in this case) and select Invalidate Cache and Restart.
    • This action clears the IDE’s cached data and forces it to reload all project dependencies, which can resolve issues caused by outdated or corrupted caches.
  3. Import Project from POM File:
    • Select the File menu again and choose New -> Project from Existing Sources.
    • In the dialog that appears, navigate to and select the pom.xml file from your project directory.
    • This action starts a fresh project import process guided by the information in your pom.xml file.
    • Ensure that you select only the pom.xml file, not the entire project directory.
  4. Wait for Project Setup:
    • Allow your IDE to automatically set up the project based on the information in the pom.xml file. This process may take a few moments.

By following these steps, you can resolve the error related to accessing the org.springframework.context.ConfigurableApplicationContext class file and successfully build and run your Spring Boot project in IntelliJ.

Solution 2: Delete the .iml file

If you are using IntelliJ and Gradle for your Spring Boot application, you may encounter the “cannot access org.springframework.context.ConfigurableApplicationContext class file for
org.springframework.context.ConfigurableApplicationContext not found” error. To resolve this issue, follow these steps:

  1. Delete the .iml file under the classpath.

This will resolve the error and allow you to run your Spring Boot application.

Solution 3: Delete main.iml file

In the child project, locate the `src/main/main.iml` file. Delete the `main.iml` file. This file is automatically generated by IntelliJ IDEA, and deleting it will not cause any permanent damage. Once the file is deleted, IntelliJ IDEA will regenerate it the next time the project is opened. This should resolve the issue and allow you to access the `org.springframework.context.ConfigurableApplicationContext` class file.

Solution 4: Use “Open” instead of “Import Project”

When using IntelliJ Idea, importing a project using the “Import Project” option may cause the error “cannot access org.springframework.context.ConfigurableApplicationContext class file for org.springframework.context.ConfigurableApplicationContext not found”.

To resolve this issue, instead of using the “Import Project” option, follow these steps:

  1. Close the project.
  2. Remove the project from IntelliJ’s project list.
  3. Use the "Open" option instead of the "Import Project" button to import the project.

This solution has been reported to work effectively in resolving the issue and allowing you to open and run the project without the mentioned error.

Solution 5: Run Maven commands and restart IDE

To resolve the issue, follow these steps:

  1. Run the command “mvn package” in the project directory using your terminal or command prompt. This command will build the project and generate the necessary JAR files.
  2. If the above step does not resolve the issue, try running the command “mvn dependency:purge-local-repository”. This command will delete all locally cached dependencies, ensuring that the latest versions are downloaded and used.
  3. After executing the commands, invalidate the caches and restart IntelliJ IDEA. This will force the IDE to refresh its dependencies and resolve any issues related to accessing the org.springframework.context.ConfigurableApplicationContext class.

After following these steps, you should be able to successfully run your Spring Boot project without encountering the “cannot access org.springframework.context.ConfigurableApplicationContext class file” error.