I am trying to get access to a folder that is created in the classpath in a Spring boot application. A snippet of the code is below:
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)); } The code above runs without any issues when I run it in Intellij and I get the url as below:
file:/C:/Users/user1/projects/my-service/test/build/resources/main/converters
When I run it on Linux inside the application rpm, I get the url value below:
jar:file:/opt/home/libexec/my-service-2.0.0-SNAPSHOT.jar!/BOOT-INF/lib/my-service-api-2.0.0-SNAPSHOT.jar!/converters
Any reason why is the different behavior?
Files, period. You have to use another strategy; which depends on the specific task at hand.