Search code examples
javaspringspring-xd

How to read a text file from spring xd module jar


I have a spring xd module, which is packaged as jar file. I want to put a text file in resources and read data from that file when the module is launched.

What I have tried so far is

   Thread.currentThread().getClass().getClassLoader().getResource("file")
   Thread.currentThread().getContextClassLoader().getResource("file")

But it didn't work. How do I read text files from module jar?


Solution

  • I have figured it out.

    You just add a member to you Tasklet class

    @Autowire
    ResourceLoader resourceLoader;
    

    And when you need to load a file from jar you just do this.

    InputStream stream = resourceLoader.getResource("/path/inside/jar/file").getInputStream();
    

    Dont' forget to close the stream :)