2

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.

Ben
  • 311
  • 2
  • 6
  • you shouldn't give the entire path starting from C:/ ... you should assume the root being used is the resource folder of your project – Stultuske Feb 01 '21 at 12:55
  • Check the permission on folder – Syed Mehtab Hassan Feb 01 '21 at 13:00
  • @Stultuske , I am not giving the path, its getting loaded from the ClassLoader, all I am providing in the argument the filename. The path from C: is whats being printed from the Jenkins console. – Ben Feb 01 '21 at 13:08
  • @SyedMehtabHassan The file being accessed is part of the cloned project, not external to the workspace. I also doublechecked and the Jenkins user has all the permissions. – Ben Feb 01 '21 at 13:15

1 Answers1

2

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.

Ben
  • 311
  • 2
  • 6