0

I tried to load file from resources folder but It throwed the exception :

java.io.FileNotFoundException: C:\PROJECTS%20FOR%20FOXMINDED\7%20TASK%20-%20SQL\target\classes\init.sql (The system cannot find the path specified)

The class where I tried to get file from resources :

public class FileLoader {

public File getFileFromResources(String fileName) {


            ClassLoader classLoader = getClass().getClassLoader();

    URL resource = classLoader.getResource(fileName);
    if (resource == null) {
        throw new IllegalArgumentException("file is not found!");
    } else {
        return new File(resource.getFile());
    }
}

}

And in the Main.class I tried like this : File initializationDbSqlFile = fileLoader.getFileFromResources("init.sql");

  • Sometimes this happens when you just added the item in the folder.If you are using IntellJ then try doing restart and invalidate from File menu. – Abhishek Sengupta Mar 10 '20 at 08:00
  • @user85421 if its a spring project, use ResourceLoader. Autowired private ResourceLoader resourceLoader; File sqlFile= resourceLoader.getResource("classpath:init.sql").getFile(); – Abhishek Sengupta Mar 10 '20 at 13:08

0 Answers0