ibrahim koz
ibrahim koz

Reputation: 629

IllegalState Failed to load ApplicationCon

I am getting my feet wet for 2 or 3 days in backend. I decided to go with Spring but I encounter the following error when I try to get build using the command line "mvn package":

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.393 s <<< FAILURE! - in com.example.demo.DemoApplicationTests
[ERROR] contextLoads  Time elapsed: 0.001 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\dev\demo\target\classes
\com\example\demo\DemoApplication.class]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [co
m.example.demo.DemoApplication] for bean with name 'demoApplication' defined in file [C:\dev\demo\target\classes\com\example\demo\DemoApplication.cla
ss]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: com/example/demo/DemoApplication has bee
n compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versio
ns up to 52.0
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.example.demo.DemoApplication] for bean with name
'demoApplication' defined in file [C:\dev\demo\target\classes\com\example\demo\DemoApplication.class]: problem with class file or dependent class; ne
sted exception is java.lang.UnsupportedClassVersionError: com/example/demo/DemoApplication has been compiled by a more recent version of the Java Run
time (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
Caused by: java.lang.UnsupportedClassVersionError: com/example/demo/DemoApplication has been compiled by a more recent version of the Java Runtime (c
lass file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors: 
[ERROR]   DemoApplicationTests.contextLoads » IllegalState Failed to load ApplicationCon...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.347 s
[INFO] Finished at: 2021-03-12T15:19:33+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project demo: There are test failures.
[ERROR]
[ERROR] Please refer to C:\dev\demo\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException**

DemoApplication.java seems as below:

package com.example.demo;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class DemoApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

DemoApplicationTests.java seems as below:

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest
@SpringBootApplication
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}

And this is my pom.xml 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.6</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.3</version>
            </plugin>
        </plugins>
    </build>
</project>

And the project directory is as seen below:

C:.
├───.idea
│   ├───codeStyles
│   └───libraries
├───.mvn
│   └───wrapper
├───src
│   ├───main
│   │   ├───java
│   │   │   └───com
│   │   │       └───example
│   │   │           └───demo
│   │   └───resources
│   └───test
│       └───java
│           └───com
│               └───example
│                   └───demo
└───target
    ├───classes
    │   └───com
    │       └───example
    │           └───demo
    ├───generated-sources
    │   └───annotations
    ├───generated-test-sources
    │   └───test-annotations
    ├───maven-status
    │   └───maven-compiler-plugin
    │       ├───compile
    │       │   └───default-compile
    │       └───testCompile
    │           └───default-testCompile
    ├───surefire-reports
    └───test-classes
        └───com
            └───example
                └───demo

My java version:

openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)

I don't know too much about Maven and Spring. It will be great if you demonstrate clearly.

Upvotes: 5

Views: 14484

Answers (2)

Hargun Singh
Hargun Singh

Reputation: 576

If you are new and not using testing, remove

@Test
void contextLoads() {
}

from the test file. This solved the issue for me.

Note: This is not a solution but a workaround for new users not using testing.

Upvotes: 5

Prabhat Yadav
Prabhat Yadav

Reputation: 1331

As per shared code snippt you re doing two thing worng with @SpringBootApplication annotation .

  1. you are not using @SpringBootApplication on the main method class(DemoApplication.java). so add the @SpringBootApplication annotation on the class DemoApplication.java
  2. you are using @SpringBootApplication on the Test Class(DemoApplicationTests) with @SpringbootTest so remove the @SpringBootApplication from the DemoApplicationTests.java.

Upvotes: 0

Related Questions