After reading this question, I have tried the following when reading a file from the classpath of a maven project built with IntelliJ:
public class testTemplateManager {
private TemplateManager templateManager;
private String m_includePath;
public void createTemplateManager() {
String relativePath = "/src/main/java/com/eprosima/templates";
String fileName = "idlTypes.stg";
String filePath = relativePath + "/" + fileName;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream res = classLoader.getResourceAsStream(var4);
if(res == null) {
classLoader = this.getClass().getClassLoader();
res = classLoader.getResourceAsStream(var4);
}
if(res != null) {
return new BufferedReader(getInputStreamReader(res));
}
}
The resource res is always null regardless of the relativePath(relative to the project root directory). I have tried to use the following paths with the same outcome:
String relativePath = "src/main/java/com/eprosima/templates";
String relativePath = "src/main/java/com.eprosima/templates";
The classpath looks like:
I need to read the template files located in /src/main/java/com/eprosima/templates.