I have few files with SQL queries in my Spring Boot project. These queries are located by path
spring-boot-sql-in-files/src/main/resources/sql/query.sql When I run my application I execute loading of these queries to static variables.
private static final String PATH_PREFIX = "src/main/resources/sql/"; public static String loadQueryFromFile(@NonNull String fileName) { try { return FileUtils.readFileToString(new File(PATH_PREFIX + fileName), Charset.defaultCharset()); } catch (Exception e) { log.error("Error occurred during loading sql file to string, file=" + fileName, e); throw new QueryNotLoadedException(); } } It works fine when I run it using IDE but it doesn't work when I run name.jar file, I get the following error:
java.io.FileNotFoundException: File 'src/main/resources/sql/query.sql' does not exist How can I fix this path?
src/main/resourcesis the root of your classpath. Which will also make thatFileUtils.readFileToStringwon't work as it isn't aFilewhen packaged as a jar. Instead use the Spring resource abstraction and utils to load it into a string./BOOT-INF/classes/sql/new ClassPathResource("/sql/query.sql").getFile();to get the file.