【发布时间】:2021-03-12 22:50:06
【问题描述】:
我正在尝试访问在 Spring Boot 应用程序的类路径中创建的文件夹。一个sn-p的代码如下:
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
URL url = classLoader.getResource("converters");
LOGGER.debug("**************** url: " + url);
File file = new File(url.toURI());
Path path = Paths.get(file.getAbsolutePath());
Arrays.stream(path.toFile().listFiles()).forEach(f -> LOGGER.debug("*******files: " + f.getName()));
if (!path.toFile().isDirectory()) {
throw new IllegalArgumentException(ErrorCode.INVALID_MAPPERS_DIRECTORY.formatMessage(path));
}
上面的代码在 Intellij 中运行时没有任何问题,我得到的 url 如下:
file:/C:/Users/user1/projects/my-service/test/build/resources/main/converters
当我在应用程序 rpm 中的 Linux 上运行它时,我得到以下 url 值:
jar:file:/opt/home/libexec/my-service-2.0.0-SNAPSHOT.jar!/BOOT-INF/lib/my-service-api-2.0.0-SNAPSHOT.jar!/converters
为什么会有不同的行为?
【问题讨论】:
-
您不能假设类路径包含文件夹或
Files,句号。你必须使用另一种策略;这取决于手头的具体任务。
标签: java spring spring-boot