[
https://issues.apache.org/jira/browse/FREEMARKER-160?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17215491#comment-17215491
]
Stefan Huber commented on FREEMARKER-160:
-----------------------------------------
After multiple hours of debugging and analyzing classpaths of different class
loaders for different packages (ClassPathHelper#forPackage(...) of
ronmamo/reflections) I figured out that
{code:java}SupportedDatabases.class.getClassLoader().getResource("bayern/steinbrecher/dbConnector/query/templates/mysql/searchQuery.ftlh");{code}
works if I do not specify a slash at the beginning whereas
{code:java}SupportedDatabases.class.getResource("/bayern/steinbrecher/dbConnector/query/templates/mysql/searchQuery.ftlh");{code}
does neither work with or without a slash at the beginning. For now it's a
little confusing for me even after reading the docs and forums. I got the
impression it's a problem related to modularization.
> Resources not found
> -------------------
>
> Key: FREEMARKER-160
> URL: https://issues.apache.org/jira/browse/FREEMARKER-160
> Project: Apache Freemarker
> Issue Type: Bug
> Affects Versions: 2.3.30
> Reporter: Stefan Huber
> Priority: Major
> Attachments: packageStructure.png
>
>
> Used technologies:
> * JDK 14
> * Gradle 6.5
> I use {code:java}setClassForTemplateLoading(SupportedDatabases.class,
> Path.get("templates", "mysql")){code} to specify the location of all my
> template files. Eventhough the files are seemingly correctly placed relative
> to the loading class (see attachment), resources are not found and the
> following error is thrown:
> {code:java}
> Caused by: freemarker.template.TemplateNotFoundException: Template not found
> for name "checkDBExistence.ftlh".
> The name was interpreted by this TemplateLoader:
> ClassTemplateLoader(resourceLoaderClass=bayern.steinbrecher.dbConnector.query.SupportedDatabases,
> basePackagePath="templates/mysql/" /* relatively to resourceLoaderClass pkg
> */).
> {code}
> I figured out that based on https://stackoverflow.com/a/14740160
> {code:java}Thread.currentThread().getContextClassLoader(){code} is a way more
> reliable choice in context of my modular projects.
> For testing the bug the following minimal setup can be used:
> - Java project (Maven or Gradle) that imports
> {code}bayern.steinbrecher:DBConnector:0.9{code}
> - A main function which consist of the following call
> {code:java}SupportedDatabases.values(){code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)