无法将java.util.List<java.lang.Object>转换为java.util.List<java.nio.file.Path>由于Jenkins中的"inco



我收到詹金斯的编译错误:

不兼容类型:java.util.List<java.lang.Object>无法转换为java.util.List<java.nio.file.Path>

在行中: collectList = dirList.collect(Collectors.toList());

在下面的方法中:

public String getMostRecentFolder(String parentFolder){                                                  
        List<Path> collectList = null;
        Path path = null;
        Path pathName = null;
        Stream<Path> dirList = null;
        try {
            dirList = Files.list(Paths.get(parentFolder)).sorted(new Comparator<Path>() {
                @Override
                public int compare(Path path1, Path path2) {
                    Long file1Name = path1.toFile().lastModified();
                    Long file2Name = path2.toFile().lastModified();
                    return file2Name.compareTo(file1Name);
                }
            });
        } catch (Exception e) {
            Log.error("getMostRecentFolder failed with exception: " + e);
        }
        try {
            collectList =  dirList.collect(Collectors.toList());
        } catch (Exception e) {
            Log.error("getMostRecentFolder failed with exception: " + e);
        }
        try {
            path = (Path) collectList.get(0);
        } catch (Exception e) {
            Log.error("getMostRecentFolder failed with exception: " + e);
        }
        try {
            pathName = path.getFileName();
        } catch (Exception e) {
            Log.error("getMostRecentFolder failed with exception: " + e);
        }       
        return pathName.toString().trim();
    }

试试这个。 我没有收到此代码的错误。我只是用"."替换了参数,用println替换了记录器,使其在main方法中运行。您可以撤消这些更改。

    List<Path> collectList = null;
    Path path = null;
    Path pathName = null;
    Stream<Path> dirList = null;
    try {
        dirList = Files.list(Paths.get(".")).sorted((p1, p2) -> ((Long) p1.toFile().lastModified()).compareTo(p2.
                toFile().lastModified()));
    } catch (Exception e) {
        System.out.println("err");
    }
    try {
        collectList = dirList.collect(Collectors.toList());
    } catch (Exception e) {
        System.out.println("err");
    }
    try {
        path = (Path) collectList.get(0);
    } catch (Exception e) {
        System.out.println("err");
    }
    try {
        pathName = path.getFileName();
    } catch (Exception e) {
        System.out.println("err");
    }
在Java 8

中,你可以使用lambdas。 如果你必须使用Java 1.8之前的某个版本然后像这样将 lambda 表达式替换为匿名内部类。

    List<Path> collectList = null;
    Path path = null;
    Path pathName = null;
    Stream<Path> dirList = null;
    try {
        dirList = Files.list(Paths.get(".")).sorted(new Comparator<Path>()
        {
            @Override
            public int compare(Path p1, Path p2)
            {
                return ((Long) p1.toFile().lastModified()).compareTo(p2.
                        toFile().lastModified());
            }
        });
    } catch (Exception e) {
        System.out.println("err");
    }
    try {
        collectList = dirList.collect(Collectors.toList());
    } catch (Exception e) {
        System.out.println("err");
    }
    try {
        path = (Path) collectList.get(0);
    } catch (Exception e) {
        System.out.println("err");
    }
    try {
        pathName = path.getFileName();
    } catch (Exception e) {
        System.out.println("err");
    }

相关内容

最新更新