Java 启用预览特性
问题
从Java 13开始支持文本块(多行字符串),可以替代从前丑陋的多行字符串拼接。即使是 Java14 该特性也是预览特性。
报错如下:Text Blocks is a preview feature and disabled by default. Use --enable-preview to enable
解决方案
eclipse
maven
修改 pom.xml 文件
<project>
...
<properties>
<java.version>13</java.version>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<arguments>--enable-preview</arguments>
<jvmArguments>--enable-preview</jvmArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
gradle
在 build.gradle 里添加
tasks.withType(JavaCompile) {
options.compilerArgs += "--enable-preview"
}
命令行
编译 javac --enable-preview -source 13 -encoding utf-8 test.java
执行 java --enable-preview test
还没有评论,来说两句吧...