Gleeb Gleeb - 7 months ago 51
Java Question

Loading freemarker templates folder inside .jar

i am using freemarker templates in my application

before deploying my application to a jar file, all i needed to do in order to load my templates was this:

cfg.setDirectoryForTemplateLoading(new File("templates"));


which loaded all templates from the template folder i created inside my project.

Now, after moving to maven and deploying my application to an executable jar. The application cannot find this folder anymore (i have checked inside the .jar file and the "templates" folder is deployed right in the root directory)

i have tried everything i know. but with no luck.

how exactly am i supposed to load all my templates now? (i assume that if i put the folder outside the .jar file but in the same directory it will work. but that's not what i want.)

Thanks.

Answer

Have a look at

void setClassForTemplateLoading(Class cl, String prefix);

...in the FreeMarker manual chapter about template loading.

Example:

cfg.setClassForTemplateLoading(this.getClass(), "/templates");

...if your templates is located in the templates package relative to the root of the current class.

Comments