[英]Spring resource class not able to find a classpath resource
我在spring boot應用程序的src/main/resources
文件夾中有一個data.sql
文件。 在文件夾src/main/java/com/rohitk/app
源代碼中,我有以下代碼。
try {
Resource res = new ClassPathResource("classpath:data.sql");
InputStream stream = res.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
在運行此程序時,我得到一個異常說明,
class path resource [classpath:data.sql] cannot be opened because it does not exist
給出絕對路徑也無濟於事。 我究竟做錯了什么
如果您使用的是ClassPathResource,則無需輸入classpath關鍵字,只需傳遞文件名即可。 通常,類路徑也意味着您的/ src / main / resources目錄。 嘗試將您的data.sql文件放在/ src / main / resources目錄中。 內部參數。 你可以這樣得到
Resource res = new ClassPathResource("/data.sql");
使用/data.sql
; 刪除其他所有內容。 另外,請確定是data.sql
還是input.sql
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.