【问题标题】:Compilation error when using Streams and Maven使用 Streams 和 Maven 时出现编译错误
【发布时间】:2015-05-03 17:34:41
【问题描述】:

我在尝试使用 Maven 编译我的代码时遇到一个奇怪的编译错误,我的代码在 Eclipse 中工作,我使用的是 Java 8Files.lines 读取文件。

[INFO] Compiling 8 source files to /Users/nilemarbarcelos/Dev/ConferenceTrackManager/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/nilemarbarcelos/Dev/ConferenceTrackManager/src/main/java/com/nilemarbarcelos/FileInputHandler.java:[29,69] incompatible types: java.util.List<java.lang.Object> cannot be converted to java.util.List<java.lang.String>
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.122 s
[INFO] Finished at: 2015-05-03T14:26:41-03:00
[INFO] Final Memory: 14M/211M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project ConferenceTrackManager: Compilation failure
[ERROR] /Users/nilemarbarcelos/Dev/ConferenceTrackManager/src/main/java/com/nilemarbarcelos/FileInputHandler.java:[29,69] incompatible types: java.util.List<java.lang.Object> cannot be converted to java.util.List<java.lang.String>
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

代码如下:

List<String> lines = null;
        try {
            URL path = getClass().getClassLoader().getResource(source);
            if (Objects.isNull(path)) {
                throw new FileNotFoundException("File not found");
            }
            lines = Files.lines(Paths.get(path.toURI())).collect(Collectors.toList());

我在哪里使用collect() 方法。

谁能帮我解决这个错误?

【问题讨论】:

  • 报告的问题核心:FileInputHandler.java:[29,69] incompatible types: java.util.List&lt;java.lang.Object&gt; cannot be converted to java.util.List&lt;java.lang.String&gt;
  • 令人惊讶的是,在我的情况下,这个错误并没有通过在编译器目标中设置版本来修复。我试过:&lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt; &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt; &lt;java.version&gt;1.8&lt;/java.version&gt;。所以我尝试将流转换为newSet = oldSet.stream().map(MyClass.class::cast).collect(Collectors.toSet()); 看起来它有效。

标签: maven java-8 java-stream collectors


【解决方案1】:

这里没有你的 POM,但你可能没有specified the JDK

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>

将其添加到 POM 的 &lt;plugins&gt; 部分并执行 mvn clean install 后,错误应该会自行解决。

【讨论】:

  • 谢谢,是这个问题
  • 当然,你“接受”解决了你的问题的答案,干杯!
【解决方案2】:

看起来你的 pom 文件被省略了maven-compiler-plugin

默认情况下,compile 阶段的 Maven 不使用 1.8 版本的 JDK。

【讨论】:

  • 谢谢,您帮助了解了发生的情况。
猜你喜欢
  • 1970-01-01
  • 2011-07-26
  • 2017-01-06
  • 2021-10-29
  • 1970-01-01
  • 2018-07-10
  • 2020-09-19
  • 1970-01-01
  • 2013-06-16
相关资源
最近更新 更多