【发布时间】:2021-05-05 15:39:06
【问题描述】:
我有一个方法调用,它读取并返回文件的内容,它在我的本地机器上运行良好 - Windows Server 2016,但是当它在我的 Jenkins 服务器上运行时 - 也生活在 Windows Server 机器上,它会抛出一个它似乎找不到文件的错误,但我能够在 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 命令执行。
【问题讨论】:
-
你不应该给出从 C:/ 开始的整个路径...你应该假设正在使用的根目录是你项目的资源文件夹
-
检查文件夹的权限
-
@Stultuske ,我没有给出路径,它是从 ClassLoader 加载的,我在参数中提供的所有文件名。来自 C: 的路径是从 Jenkins 控制台打印的内容。
-
@SyedMehtabHassan 正在访问的文件是克隆项目的一部分,而不是工作区外部。我还仔细检查了 Jenkins 用户拥有所有权限。
标签: java jenkins nosuchfileexception