簡體   English   中英

Spring資源類無法找到類路徑資源

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM