简体   繁体   English

Spring资源类无法找到类路径资源

[英]Spring resource class not able to find a classpath resource

I have a data.sql file located in my src/main/resources folder inside my spring boot application. 我在spring boot应用程序的src/main/resources文件夹中有一个data.sql文件。 In my source code inside the folder src/main/java/com/rohitk/app I have the following code. 在文件夹src/main/java/com/rohitk/app源代码中,我有以下代码。

        try {
            Resource res = new ClassPathResource("classpath:data.sql");
            InputStream stream = res.getInputStream();

        } catch (Exception e) {
            e.printStackTrace();
        }

On running this I get an exception stating, 在运行此程序时,我得到一个异常说明,

class path resource [classpath:data.sql] cannot be opened because it does not exist

Giving absolute path also does not help. 给出绝对路径也无济于事。 What am I doing wrong 我究竟做错了什么

If you are using ClassPathResource you don't need to enter classpath keyword, pass file name only. 如果您使用的是ClassPathResource,则无需输入classpath关键字,只需传递文件名即可。 And also usually class path means your /src/main/resources directory. 通常,类路径也意味着您的/ src / main / resources目录。 try place your data.sql file inside /src/main/resources directory. 尝试将您的data.sql文件放在/ src / main / resources目录中。 inside parameter. 内部参数。 You can get it like this, 你可以这样得到

Resource res = new ClassPathResource("/data.sql");

Use /data.sql ; 使用/data.sql ; remove everything else. 删除其他所有内容。 Also, make up your mind whether it's data.sql or input.sql 另外,请确定是data.sql还是input.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM