How to read a file from jar in Java?

Viewed 151985

I want to read an XML file that is located inside one of the jars included in my class path. How can I read any file which is included in the jar?

6 Answers

This also works on spring

    ClassPathResource resource = new ClassPathResource("/file.txt", MainApplication.class); //resources folder
    InputStream inputStream = resource.getInputStream();

    File file = new File("file.txt");
    FileUtils.copyInputStreamToFile(inputStream, file);
Related