AHHP
AHHP

Reputation: 3047

Read relative static file in src

I have .tpl file containing some static content in a package in src. e.g. src/A/B/C/test.tpl and i'm trying to read it from a class sibling that file (src/A/B/C/Test).

I can't find it in any way! FileReader throws FileNotFoundException.

SOLUTION: Class.getResource() works. Problem is about tpl extension which will not be compiled by default. IDEs have setting to add extensions to compile. I used .html instead of updating compiler settings.

Test.class.getResource("/A/B/C/test.html").getPath().replace("%20", " ")

Upvotes: 2

Views: 187

Answers (2)

Mukus
Mukus

Reputation: 5033

How about moving the file to src/main/resources and then trying something like -

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/test.tpl");

    try {

        context.getClassLoader().getResourceAsStream("test.tpl");

       ...
        } 
   catch (IOException ex) {
        ex.printStackTrace();
    }

Upvotes: 0

Aviram Segal
Aviram Segal

Reputation: 11120

You should use ClassLoader.html#getResourceAsStream

getClassloader().getResourceAsStream(resourcePath);

Upvotes: 3

Related Questions