Reputation: 1715
I was trying to read sub-directories(names of directories) of a directory in my class path resources using Spring Boot with snippet given below.
public List<File> getSubdirectories() {
File file = ResourceUtils.getFile("classpath:/database/scripts");
return Arrays.stream(file.listFiles()).filter(File::isDirectory).map(File::getFileName)
.collect(Collectors.toList());
}
Now this would work well when we run this code from IDE. But when we build this app as a jar and runs it from there it throws a FileNotFoundException.
I tried to load the directory as class path resource as well. But didn't find any luck.
I'm currently using Java 8. So most of the Java 7 solutions won't work I guess. Is there any other way I can resolve this issue?
Upvotes: 0
Views: 1771
Reputation: 940
As per Spring documentation ResourceUtils are to be used internally within the framework
Try using Resource instead:
@Value("classpath:database/scripts")
Resource directories;
Alternatively, you can use resource loader:
@Autowired
ResourceLoader resourceLoader;
...
public Resource loadDirectories() {
return resourceLoader.getResource(
"classpath:database/scripts");
}
Upvotes: 1