【发布时间】:2021-08-22 06:23:47
【问题描述】:
我有 2 台开发电脑,都有以下规格:
- 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
所以,解决方法是点击上图Add...,然后选择已安装的JDK(C:\Program Files\Java\jdk1.8.0_291)。
更新 2
这是对@OneCricketeer 的反馈。
从计算机上卸载了 JDK,但 Eclipse 仍然可以运行 Java 项目。
【问题讨论】:
-
让我猜猜,不同的java版本一个使用JDK9+,而另一个使用JDK8。 JDK9+ 从 JDK 中移除了 jaxb,而之前它仍然是 JDK 的一部分。
-
@M.Deinum 您的客人是正确的,谢谢。在我的 PC1 上,安装了 JDK8,但 Eclipse 仍然使用 Eclipse ZIP 文件中附带的默认 Java 15。
-
上次我检查过,Eclipse 没有附带任何 JDK
-
@OneCricketeer,请参阅我更新的帖子。 Eclipse IDE (2021-03) 实际上带有“内置”JRE 15。
-
那是为了运行IDE本身,但是JRE不能编译任何项目