111

I have the following directory layout:

  • src
    • main
      • java
      • resources
        • sql (scripts for database)
        • spring (configuration)
      • webapp

Within a ServletContextListener class, I want to access the files under the SQL directory and list them. Basically my problem is with the path, because I know that listing files under a directory in a nutshell is:

File folder = new File(path);
File[] listOfFiles = folder.listFiles();

Maybe I could use the ServletContextEvent Object to try and build a path to resources/sql

public void contextInitialized(ServletContextEvent event) {
    event.getServletContext(); //(getRealPath etc.)
}

Does something exist to set that path in a relative, non-hardcoded way? Something like new File("classpath:sql") (preferably spring if possible) or what should I do with the servletContext to point at resources/sql?

2

4 Answers 4

86

I'm assuming the contents of src/main/resources/ is copied to WEB-INF/classes/ inside your .war at build time. If that is the case you can just do (substituting real values for the classname and the path being loaded).

URL sqlScriptUrl = MyServletContextListener.class
                       .getClassLoader().getResource("sql/script.sql");
5
  • Thanks!! this worked for me, with that URL then I build the Path for the new File, and finally get the files in that directory. Oct 17, 2013 at 15:25
  • 2
    You don't need the File. You already have a URL. You can't even assume there is a file or a directory at all. The WAR may not have been unpacked.
    – user207421
    Oct 18, 2013 at 21:24
  • What does MyServletContextListener mean here ? Dec 4, 2015 at 10:02
  • @TejeshRaut It is a place holder for a class that implements javax.servlet.ServletContextListener. I used that as an example because the question had a snippet from contextInitialized which is a method defined by the ServletContextListener interface.
    – Dev
    Dec 5, 2015 at 1:24
  • For me it was crucial to prepend the path with classpath:, so: "classpath:/sql/script.sql".
    – isgoed
    Jan 21, 2021 at 15:19
76

Finally, this is what I did:

private File getFileFromURL() {
    URL url = this.getClass().getClassLoader().getResource("/sql");
    File file = null;
    try {
        file = new File(url.toURI());
    } catch (URISyntaxException e) {
        file = new File(url.getPath());
    } finally {
        return file;
    }
}

...

File folder = getFileFromURL();
File[] listOfFiles = folder.listFiles();
3
  • 12
    Are you sure of using "/sql" as the parameter? I have problems with a starting slash, Instead "sql" works.
    – Abdull
    May 13, 2016 at 13:57
  • 2
    Please be aware that you can get a different URL with this.getClass().getResource("/sql")); Oct 15, 2018 at 13:59
  • This won't work on all containers. Tomcat, for example.
    – user207421
    Feb 11, 2019 at 23:10
19
import org.springframework.core.io.ClassPathResource;

...

File folder = new ClassPathResource("sql").getFile();
File[] listOfFiles = folder.listFiles();

It is worth noting that this will limit your deployment options, ClassPathResource.getFile() only works if the container has exploded (unzipped) your war file.

11

Just use com.google.common.io.Resources class. Example:

 URL url = Resources.getResource("file name")

After that you have methods like: .getContent(), .getFile(), .getPath() etc

1
  • 3
    Do they also support getting resources while the code is being executed from a jar file?
    – Displee
    Dec 22, 2019 at 15:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.