Reputation: 3047
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
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
Reputation: 11120
You should use ClassLoader.html#getResourceAsStream
getClassloader().getResourceAsStream(resourcePath);
Upvotes: 3