使用Streams和Maven时出现编译错误
在尝试使用Maven编译代码时,我遇到了一个奇怪的编译错误,我的代码在Eclipse中工作,我使用Java8和使用Streams和Maven时出现编译错误,maven,java-8,java-stream,collectors,Maven,Java 8,Java Stream,Collectors,在尝试使用Maven编译代码时,我遇到了一个奇怪的编译错误,我的代码在Eclipse中工作,我使用Java8和Files.lines读取文件 [INFO] Compiling 8 source files to /Users/nilemarbarcelos/Dev/ConferenceTrackManager/target/classes [INFO] ------------------------------------------------------------- [ERROR] CO
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
[INFO]将8个源文件编译为/Users/nilemarcelos/Dev/ConferenceTrackManager/target/classes
[信息]-------------------------------------------------------------
[错误]编译错误:
[信息]-------------------------------------------------------------
[错误]/Users/nilemarbarcelos/Dev/ConferenceTrackManager/src/main/java/com/nilemarbarcelos/FileInputHandler.java:[29,69]不兼容的类型:java.util.List无法转换为java.util.List
[INFO]1错误
[信息]-------------------------------------------------------------
[信息]------------------------------------------------------------------------
[信息]生成失败
[信息]------------------------------------------------------------------------
[信息]总时间:2.122秒
[信息]完成时间:2015-05-03T14:26:41-03:00
[信息]最终内存:14M/211M
[信息]------------------------------------------------------------------------
[错误]无法在project ConferenceTrackManager上执行目标org.apache.maven.plugins:maven编译器插件:3.1:compile(默认编译):编译失败
[错误]/Users/nilemarbarcelos/Dev/ConferenceTrackManager/src/main/java/com/nilemarbarcelos/FileInputHandler.java:[29,69]不兼容的类型:java.util.List无法转换为java.util.List
[错误]->[帮助1]
[错误]
[错误]要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。
[错误]使用-X开关重新运行Maven以启用完整调试日志记录。
[错误]
[错误]有关错误和可能的解决方案的更多信息,请阅读以下文章:
[错误][帮助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());
列表行=null;
试一试{
URL路径=getClass().getClassLoader().getResource(源);
if(Objects.isNull(路径)){
抛出新的FileNotFoundException(“未找到文件”);
}
lines=Files.lines(path.get(path.toURI()).collect(Collectors.toList());
我使用的是collect()
方法
有人能帮我解决这个错误吗?您这里没有POM,但您可能没有:
org.apache.maven.plugins
maven编译器插件
3.3
1.8
1.8
一旦您将其添加到POM的
部分并执行mvn干净安装
,错误应该会自行解决。看起来POM文件中省略了maven编译器插件
默认情况下,Maven for
compile
stage不使用JDK的1.8版本。报告的问题核心是:FileInputHandler.java:[29,69]不兼容的类型:java.util.List无法转换为java.util.List
令人惊讶的是,在我的情况下,在编译器目标中设置版本并不能修复此错误。我尝试了:1.8 1.8 1.8
。因此我尝试将流转换为newSet=oldSet.stream().map(MyClass.class::cast).collector.toSet())
看起来好像成功了。谢谢,这就是问题所在。当然,谢谢你“接受”了解决问题的答案,干杯!谢谢你,你帮我找出了问题所在。
<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>