Daniel
Thanks for taking time to respond.
The problem I was having is that the "templates/testEmailTemplate.ftl" which
is in a jar file wasn't getting loaded when I used ClassTemplateLoader.
log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.io.FileNotFoundException: Template
testEmailTemplate.ftl not found.
at
freemarker.template.Configuration.getTemplate(Configuration.java:489)
at
freemarker.template.Configuration.getTemplate(Configuration.java:452)
Upon digging up ClassTemplateLoader {free marker v2.3.9} source, I found
getUrl() to be defined as follows.
protected URL getURL(String name)
{
return loaderClass.getResource(path + name);
}
It seems valid. However this wasn't loading the .ftl file from templates
folder. I explicitly initialized ClassLoader as shown the code in the
previous email and it worked.
I understand the intent of this class but wasn't able to make it work for me
so I used that class loader to load the resource.
Thank you,
Ashwin
*****This e-mail and replies and forwards are for the sole use of the above
individual(s) or entities and may contain proprietary, privileged and/or
highly confidential information. Unauthorized use is strictly
prohibited.*****
From: "Daniel Dekany [via FreeMarker]"
<ml-node+***@n4.nabble.com>
Date: Thursday, February 21, 2013 6:20 PM
To: Ashwini Kuntamukkala <***@scispike.com>
Subject: Re: Loading templates from classpath ( ClassTemplateLoader problem
or /dev/hands ? )
Post by akuntamukkalaHere is what I did to get around this problem with ClassTemplateLoader
The issue is that the proper class loader isn't being used. I got around
this by subclassing ClassTemplateLoader
public class FixedClassTemplateLoader extends ClassTemplateLoader {
private ClassLoader classLoader;
public FixedClassTemplateLoader(Class clazz, String string) {
super(clazz, string);
classLoader = clazz.getClassLoader();
}
@Override
protected URL getURL(String name) {
// this now loads the template file from the jar
return classLoader.getResource(name);
}
}
So, what exactly does this fix and how? As far as I see,
ClassTemplateLoader already uses the defining class-loader of the
`clazz` constructor argument, so there's no change here. Or is there?
BTW, the other change is that you ignore the 2nd constructor parameter
(the base path) in the getURL method for some reason, although if you
really don't want a base path, you could just use "" for it as the
constructor argument.
--
Best regards,
Daniel Dekany
Post by akuntamukkalathen here is how I used it
ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(),
"templates");
TemplateLoader[] loaders = new TemplateLoader[] { ctl };
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
cfg.setTemplateLoader(mtl);
Template template =
cfg.getTemplate("templates/testEmailTemplate.ftl");
----
Now I am able to load the template and generate my email template.
Thanks
----------------------------------------------------------------------------
--
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
FreeMarker-user mailing list
[hidden email] </user/SendEmail.jtp?type=node&node=4654454&i=0>
https://lists.sourceforge.net/lists/listinfo/freemarker-user
If you reply to this email, your message will be added to the discussion
below:
http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-Clas
sTemplateLoader-problem-or-dev-hands-tp625098p4654454.html
To unsubscribe from Loading templates from classpath ( ClassTemplateLoader
problem or /dev/hands ? ), click here
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=unsub
scribe_by_code&node=625098&code=YXNod2luLmt1bnRhbXVra2FsYUBzY2lzcGlrZS5jb218
NjI1MDk4fC0xMTYyMDA0NzAz> .
NAML
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=macro
_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.Ba
sicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.templa
te.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instan
t_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
--
View this message in context: http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-ClassTemplateLoader-problem-or-dev-hands-tp625098p4654455.html
Sent from the freemarker-user mailing list archive at Nabble.com.