Jenkins throws NoSuchFileException when file exists in workspace

2.4k Views Asked by At

I have a method call which reads and returns the contents of a file, which works fine on my local box - Windows Server 2016, but when it runs on my Jenkins server - also living on a Windows Server box, it throws an error that it seemingly can't find the file, but I am able to find the file in the Jenkins workspace with the exact path printed in the console:

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)

The method causing the issue is this:

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)));
        }
    }

Both my local and Jenkins are being executed with the same maven command as well.

1

There are 1 best solutions below

0
On

I figured out the issue.

My Jenkins project name has spaces, so when the code looks for the path, it can't find it since the spaces were replaced by the ASCI encoding '%20'. Simplest solution was to change my project name an exclude the spaces. If I really wanted to I can wrap the whole path with quotes which would probably also work.