簡體   English   中英

當工作空間中存在文件時,Jenkins 拋出 NoSuchFileException

[英]Jenkins throws NoSuchFileException when file exists in workspace

我有一個讀取並返回文件內容的方法調用,它在我的本地機器上運行良好 - Windows Server 2016,但是當它在我的 Jenkins 服務器上運行時 - 也生活在 ZAEA23489CE3AA9B6406EBB28E0CDA4 服務器上,它拋出了一個錯誤似乎找不到該文件,但我能夠在 Jenkins 工作區中找到該文件,並在控制台中打印了確切的路徑:

12:05:24 [Utils] [ERROR] [Error] java.nio.file.NoSuchFileException: C:\Users\****\AppData\Local\Jenkins\.jenkins\workspace\Maven%20Project%20Test\gems_automation\gems\GEMS\target\classes\getPM.sql
12:05:24    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
12:05:24    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
12:05:24    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
12:05:24    at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:235)
12:05:24    at java.base/java.nio.file.Files.newByteChannel(Files.java:371)
12:05:24    at java.base/java.nio.file.Files.newByteChannel(Files.java:422)
12:05:24    at java.base/java.nio.file.Files.readAllBytes(Files.java:3206)
12:05:24    at com.gems.testsuite.base.TestBaseUtils.readResourceFile(TestBaseUtils.java:23)

導致問題的方法是這樣的:

public String  readResourceFile(String fileName) throws IOException {
        ClassLoader classLoader = getClass().getClassLoader();
        URL resource = classLoader.getResource(fileName);
        if (resource == null) {
            throw new IllegalArgumentException("file is not found!");
        } else {
            File file = new File(resource.getFile());
            Path path = Paths.get(FilenameUtils.separatorsToSystem(file.getPath()));
            return new String((Files.readAllBytes(path)));
        }
    }

我的本地和 Jenkins 也正在使用相同的 maven 命令執行。

我弄清楚了這個問題。

我的 Jenkins 項目名稱有空格,所以當代碼查找路徑時,它找不到它,因為空格被 ASCI 編碼“%20”替換。 最簡單的解決方案是更改我的項目名稱並排除空格。 如果我真的想這樣做,我可以用引號將整個路徑包裹起來,這可能也可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM