Java不允许我使用records。

huangapple go评论104阅读模式
英文:

Java is not letting me use records

问题

  1. 我正在运行 JDK 15刚刚下载了它),并且在尝试使用记录records时遇到编译错误
  2. public class Runner {
  3. // 这会导致编译错误
  4. record Point(int x, int y) {}
  5. }

以下是我的 pom.xml 文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>something</groupId>
  7. <artifactId>other-thing</artifactId>
  8. <version>1.0.0-SNAPSHOT</version>
  9. <properties>
  10. <java.version>15</java.version>
  11. </properties>
  12. <build>
  13. <plugins>
  14. <plugin>
  15. <groupId>org.apache.maven.plugins</groupId>
  16. <artifactId>maven-compiler-plugin</artifactId>
  17. <version>3.8.1</version>
  18. <configuration>
  19. <source>${java.version}</source>
  20. <target>${java.version}</target>
  21. </configuration>
  22. </plugin>
  23. </plugins>
  24. </build>
  25. </project>
  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m running JDK 15 (Just downloaded it) and I&#39;m getting a compile error when I try to use records:
  4. public class Runner {
  5. // This gives a compile error
  6. record Point(int x, int y) {}
  7. }
  8. Here is my pom.xml:
  9. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  10. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  11. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  12. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  13. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  14. &lt;groupId&gt;something&lt;/groupId&gt;
  15. &lt;artifactId&gt;other-thing&lt;/artifactId&gt;
  16. &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  17. &lt;properties&gt;
  18. &lt;java.version&gt;15&lt;/java.version&gt;
  19. &lt;/properties&gt;
  20. &lt;build&gt;
  21. &lt;plugins&gt;
  22. &lt;plugin&gt;
  23. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  24. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  25. &lt;version&gt;3.8.1&lt;/version&gt;
  26. &lt;configuration&gt;
  27. &lt;source&gt;${java.version}&lt;/source&gt;
  28. &lt;target&gt;${java.version}&lt;/target&gt;
  29. &lt;/configuration&gt;
  30. &lt;/plugin&gt;
  31. &lt;/plugins&gt;
  32. &lt;/build&gt;
  33. &lt;/project&gt;
  34. </details>
  35. # 答案1
  36. **得分**: 3
  37. 记录在Java 15中仍然是一个预览功能,因此在运行编译器时需要添加`--enable-preview`参数:
  38. ```xml
  39. <plugin>
  40. <groupId>org.apache.maven.plugins</groupId>
  41. <artifactId>maven-compiler-plugin</artifactId>
  42. <version>3.8.1</version>
  43. <configuration>
  44. <source>${java.version}</source>
  45. <target>${java.version}</target>
  46. <compilerArgs>
  47. <arg>--enable-preview</arg>
  48. </compilerArgs>
  49. </configuration>
  50. </plugin>
英文:

Records are still a preview feature in Java 15, so you will need to add the --enable-preview argument when running the compiler:

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  3. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;3.8.1&lt;/version&gt;
  5. &lt;configuration&gt;
  6. &lt;source&gt;${java.version}&lt;/source&gt;
  7. &lt;target&gt;${java.version}&lt;/target&gt;
  8. &lt;compilerArgs&gt;
  9. &lt;arg&gt;--enable-preview&lt;/arg&gt;
  10. &lt;/compilerArgs&gt;
  11. &lt;/configuration&gt;
  12. &lt;/plugin&gt;

huangapple
  • 本文由 发表于 2020年9月29日 07:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64110887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定