IllegalState无法加载ApplicationCon



我的脚在后台湿了两三天。我决定使用Spring,但当我尝试使用命令行"进行构建时,我遇到了以下错误;mvn包":

[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:devdemotargetclasses
comexampledemoDemoApplication.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:devdemotargetclassescomexampledemoDemoApplication.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:devdemotargetclassescomexampledemoDemoApplication.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:devdemotargetsurefire-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如下所示:

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如下所示:

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() {
}
}

这是我的pom.xml文件。

<?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>

项目目录如下:

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

我的java版本:

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)

我对Maven和Spring了解不多。如果你能清楚地展示,那就太好了。

如果您是新手并且没有使用测试,请删除

@Test
void contextLoads() {
}

来自测试文件。这为我解决了问题。

注意:这不是一个解决方案,而是为不使用测试的新用户提供的解决方案。

根据共享代码snippt,您正在使用@SpringBootApplication注释做两件事。

  1. 您不是在主方法类(DemoApplication.java(上使用@SpringBootApplication。因此,在类DemoApplication.jar上添加@SpringBootApplication注释
  2. 您在带有@SpringbootTest的测试类(DemoApplicationTests(上使用@SpringBootApplication,因此请从DemoApplicationTests.java中删除@SpringBootApplication

最新更新