我在 IntelliJ 中使用 Gradle 创建了一个使用 Java 17 的新 Java 项目。运行我的应用程序时出现错误Cause: error: invalid source release: 17
。
我的设置
我已经安装openjdk-17
完毕IntelliJ
并将其设置为我的Project SDK
.
已Project language level
设置为17 - Sealed types, always-strict floating-point semantics
.
在Modules -> Sources
我已经设置Language level
为Project default (17 - Sealed types, always strict floating-point semantics)
.
在Modules -> Dependencies
我已经设置Module SDK
为Project SDK openjdk-17
.
在Settings -> Build, Execution, Deployment -> Compiler -> Java …
我们正在升级 docker 中的微服务以使用 Java 17,之前我们使用基础镜像 openjdk:11-jre-slim。Java 17对应的镜像是什么?
好像没有openjdk:17-jre-slim?事实上,似乎没有任何最新的 jre 映像 - 只有 jdks。11-jre-slim 映像似乎约为 75MB - 是否有合适的类似大小的 Java 17 映像?
我们过去也使用过高山图像。
我最近想将我的 kotlin 项目从 spring boot 2.7.x 升级到 3.0.1。我使用 Java 17 temurin,gradle 7.6。在 IntelliJ 中,我在通过 gradle 导入项目时收到以下错误消息:
A problem occurred configuring root project 'demo'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.1.
Required by:
project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.1
> No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.1 was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' …
Run Code Online (Sandbox Code Playgroud) 今天,我将Java版本从16更新到17,我发现sealed
类是其中的一个新功能。我认为可以这样声明:
public sealed class Main permits AClass, AnotherClass {
}
Run Code Online (Sandbox Code Playgroud)
但是,Java 中的密封类有什么用呢?
我还知道这是JDK 15中的预览功能。
在Running unit test with Spark 3.3.0 on Java 17 failed with IllegalAccessError: class StorageUtils cannot access class sun.nio.ch.DirectBuffer 中提出了类似的问题,但该问题(和解决方案)仅与单元测试有关。对我来说 Spark 正在破坏程序的实际运行。
根据Spark 概述,Spark 可与 Java 17 配合使用。我在 Windows 10 上使用 Temurin-17.0.4+8 (build 17.0.4+8),包括 Maven 中的 Spark 3.3.0,如下所示:
<scala.version>2.13</scala.version>
<spark.version>3.3.0</spark.version>
...
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我尝试运行一个简单的程序:
final SparkSession spark = SparkSession.builder().appName("Foo Bar").master("local").getOrCreate();
final Dataset<Row> df = spark.read().format("csv").option("header", "false").load("/path/to/file.csv");
df.show(5);
Run Code Online (Sandbox Code Playgroud)
这到处都是:
Caused by: java.lang.IllegalAccessError: class org.apache.spark.storage.StorageUtils$ (in unnamed module @0x59d016c9) cannot access class …
Run Code Online (Sandbox Code Playgroud) 我正在开发一款在 Java 16 上运行的 Jetty Web 应用程序。我尝试将其升级到 Java 17,但完全由一次调用parallelStream()
.
唯一的变化是 Java 版本从 16 提升到 17,--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED
运行时openjdk:16.0.1-jdk-oraclelinux8
从openjdk:17.0.1-jdk-oraclelinux8
.
我们设法获得了一个线程转储,其中包含许多内容:
"qtp1368594774-200" #200 prio=5 os_prio=0 cpu=475.94ms elapsed=7189.65s tid=0x00007fd49c50cc10 nid=0xd1 waiting on condition [0x00007fd48fef7000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@17.0.1/Native Method)
- parking to wait for <0x00000007b73439a8> (a java.util.stream.ReduceOps$ReduceTask)
at java.util.concurrent.locks.LockSupport.park(java.base@17.0.1/LockSupport.java:341)
at java.util.concurrent.ForkJoinTask.awaitDone(java.base@17.0.1/ForkJoinTask.java:468)
at java.util.concurrent.ForkJoinTask.invoke(java.base@17.0.1/ForkJoinTask.java:687)
at java.util.stream.ReduceOps$ReduceOp.evaluateParallel(java.base@17.0.1/ReduceOps.java:927)
at java.util.stream.AbstractPipeline.evaluate(java.base@17.0.1/AbstractPipeline.java:233)
at java.util.stream.ReferencePipeline.collect(java.base@17.0.1/ReferencePipeline.java:682)
at com.stackoverflowexample.aMethodThatDoesBlockingIOUsingParallelStream()
Run Code Online (Sandbox Code Playgroud)
导致问题的代码类似于:
list.parallelStream()
.map(this::callRestServiceToGetSomeData)
.collect(Collectors.toUnmodifiableList());
Run Code Online (Sandbox Code Playgroud)
此图显示了从 jdk16(左轴)升级到 jdk17(中间的巨大尖峰),然后删除对parallelStream()
仍然在 jdk17(右轴)上的调用之前的线程使用情况:
Java 17 (openjdk-17.0.1_linux-x64_bin.tar.gz) …
Spring boot升级报错
Spring Boot应用程序升级到2.6.0后由于循环依赖而无法启动
Error creating bean with name 'securityConfig':
Requested bean is currently in creation: Is there an
unresolvable circular reference?
Run Code Online (Sandbox Code Playgroud) 我在 JDK 19 上使用 Java 语言级别 17。我有
package ocp17.ch07;
public record BeardedDragon(boolean fun) {
@Override
public boolean fun() {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么没有什么可以覆盖,但仍然可以放置@Override
而没有语法错误?
以下代码片段无法在 javac 版本 17 (Temurin) 上编译
class Instanceof {
static void doesNotWork(Object o) {
if (o == null) {
throw new Error();
} else if (!(o instanceof String s)) {
throw new Error();
}
System.out.println(s); // error here
}
}
Run Code Online (Sandbox Code Playgroud)
它会生成此错误:找不到符号
cannot find symbol
symbol: variable s
location: class Instanceof
Run Code Online (Sandbox Code Playgroud)
然而,以下(在我看来)等效的变体是有效的: 使用显式的 else 块:
static void doesWork(Object o) {
if (o == null) {
throw new Error();
} else if (!(o instanceof String s)) {
throw new Error();
} else …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的 SpringBoot maven 项目更新到 Java 17。
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Run Code Online (Sandbox Code Playgroud)
我定义了工作 maven-jaxb2-plugin 插件,它从 XSD 文件成功创建了 java 类。
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<executions>
<execution>
<id>my_schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/my/files</schemaDirectory>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
<bindingDirectory>src/main/resources/my/files</bindingDirectory>
<bindingIncludes>
<include>bindings.xml</include>
</bindingIncludes>
<args>
<arg>-extension</arg>
<arg>-Xnamespace-prefix</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
但自从java和插件升级后我就不能这样做了。
我定义了插件必要的依赖项:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但在 Maven 构建过程中我仍然收到未知错误:
Oct 19, 2021 10:07:06 PM com.sun.xml.bind.v2.runtime.reflect.opt.Injector <clinit> …
Run Code Online (Sandbox Code Playgroud) java-17 ×10
java ×8
spring-boot ×3
gradle ×2
apache-spark ×1
class ×1
docker ×1
java-stream ×1
jetty ×1
maven ×1
sealed-class ×1