问题描述
我有 2 台开发 PC,都具有以下规格:
- Windows 10 1909 64 位
- 已安装 Oracle JDK 1.8.0_281-b09
- 面向 Java 开发人员的 Eclipse IDE 2020-12 (4.18.0)
正在开发的应用程序是一个 Spring Boot 项目,包含以下 Maven 文件:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my_company.example</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4-rule-agent -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-javaagent -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-javaagent</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>core</module>
<module>module1</module>
<module>module2</module>
</modules>
</project>
在将项目导入 Eclipse 时,其中一台开发 PC(称为 PC1)无法构建并出现以下错误:
在另一端PC(称为PC2)上,没有发生错误,它可以成功构建和运行。
为了解决该错误,我们在 POM.xml 文件中添加了以下依赖项:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
但是我想问一下为什么在PC1中需要显式添加依赖,而在PC2中则不需要?
后来,我还从 Eclipse (Run As > Maven Build... > select "package" in Goals field
) 构建了 JAR+WAR 文件。
然后将 JAR+WAR 文件部署到 Docker 容器(基础镜像为 tomcat:9.0.11-jre8
)。
出现了类似的问题,从PC1 构建的JAR 文件无法运行,而从PC2 构建的JAR 文件仍然可以运行。错误信息:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method Failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
我也认为问题来自上述PC1。 任何人都可以帮助检查这些问题是否有任何可能的原因?
更新
根据 @M.Deinum
和 @OneCricketeer
的回答,我解决了这个问题。
在 PC1 中,我安装了 JDK8,并添加到 PATH。
Eclipse IDE 可从以下链接下载:
https://www.eclipse.org/downloads/download.PHP?file=/technology/epp/downloads/release/2021-03/R/eclipse-java-2021-03-R-win32-x86_64.zip
检查配置时,它仍然使用默认的Java 15,如下所示:
所以,解决方法是点击上图中的Add...
,然后选择安装的JDK(C:\Program Files\Java\jdk1.8.0_291
)。
更新 2
这是对@OneCricketeer
的反馈。
从电脑上卸载了 JDK,但是 Eclipse 仍然可以运行 Java 项目。
解决方法
已安装 Oracle JDK 1.8.0_281-b09
一些东西
-
其中一台计算机没有使用 Java 8 编译代码,否则 jaxb 将可用
-
除非您维护旧代码,否则您至少应该使用 Java 11
-
如果您绝对需要 Java 8,最好迁移到 OpenJDK 而不是 Oracle 的发行版
如果是一个全新的项目,你可以使用任何更新的Java版本,但值得指出的是Spring Boot版本也应该更新