问题描述
我有一个包含Scala(2.13.1)和Java文件的sbt项目。
我想使用Java 14编译Java文件,包括preview features。
我该怎么办?
在上述oracle文档之后,我尝试设置javaoptions + javacoptions:
javaoptions ++= Seq("--enable-preview"),// when running
javacoptions ++= Seq("--enable-preview","--release","14"),// when compiling
package example;
public record Person(String name,Integer age) {}
我得到:
[错误] /Users/dvir/learn/playground/src/main/java/example/Person.java:3:15:类型声明的非法启动
[错误]公共记录Person(字符串名称,整数年龄){}
[错误] ^
注意:
我在Mac上使用AdoptOpenJDK 14。
我的JAVA_HOME
是/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
。
使用jshell
时,我可以毫无问题地使用预览功能:
dvir@Mac ~/learn/playground$ jshell --enable-preview
| Welcome to JShell -- Version 14.0.2
| For an introduction type: /help intro
jshell> public record Person(String name,Integer age) {}
| created record Person
jshell> var alice = new Person("alice",20)
alice ==> Person[name=alice,age=20]
jshell>
当我注释出Person record
(因此仅使用Java 14中的标准功能,例如switch表达式)但保留javaoptions
和javacoptions
时,仍然收到此警告: / p>
[警告]从类文件中读取API时出错:example.MainJava:java.lang.UnsupportedClassversionError:未启用example / MainJava的预览功能(类文件版本58.65535)。尝试使用'--enable-preview'
当我同时注释掉javaoptions
和javacoptions
时,java代码将编译并按预期运行。
请注意,上面的编译器警告类似于标题Forced To ––enable–preview At Run Time
下的警告in this blog。
警告和博客都建议像我一样添加––enable–preview
。
如何设置sbt以使用Java 14预览功能?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)