You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by "Dániel Dékány (Jira)" <ji...@apache.org> on 2020/10/11 18:58:00 UTC

[jira] [Commented] (FREEMARKER-160) Resources not found

    [ https://issues.apache.org/jira/browse/FREEMARKER-160?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17212006#comment-17212006 ] 

Dániel Dékány commented on FREEMARKER-160:
------------------------------------------

You happen to know why it doesn't work, given that the class and the resources are in the same jar (or I suppose so).

As of using getContextClassLoader, the tricky thing is that then what a template name resolves to can depend on the thread. That's a problem for caching, which caches by template name. Actually, it can realize that the class loader is different, and then not use the cached entry, but then we are will keep dropping the cache entries as the calls from the various threads come. In reality though, probably the class loader will be always the same, as all threads belong to the same web application... It's still not easy to do this correctly. Also if the class loader is always the same among the different threads, then we are back to the original question: why it doesn't work if you just use the class loader of a class from the same jar as the resources?

> 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)