maven - 使用Streams和Maven时编译错误

标签 maven java-8 java-stream collectors

尝试使用 编译我的代码时遇到一个奇怪的编译错误Maven ,我的代码在 Eclipse 中工作,我使用的是 Java 8 Files.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()方法。

谁能帮我解决这个错误?

最佳答案

你这里没有你的 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>

将它添加到 <plugins> 后您的 POM 部分并执行 mvn clean install ,错误应该会自行解决。

关于maven - 使用Streams和Maven时编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30017102/

相关文章:

java - 尝试构建一维数组时,推理变量具有不兼容的边界

maven - jasperreports-maven-plugin 的稳定版本是什么?

java - 在命令行中创建 Maven 项目

Java 8 流经多个层并连接最底层的所有项目

java - 解释 Java 8 收集器接口(interface)/方法签名

java - 从功能接口(interface)中的java 8 Map Reduce中断或返回

java - 如何针对不同版本的客户端代码进行自动化测试

java - 父项目的打包类型必须是POM

java.util.Optional 和 java.lang.Class - 奇怪的行为

java - 扫描文件并收集与模式匹配的完整单词