You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mark Fenbers <Ma...@noaa.gov> on 2009/06/14 01:56:01 UTC
Resource from a jar file
I'm having some trouble with mergeTemplate() in that it seems to be
unable to open a template file if it is packaged in a jar file. In my
Java code, I use getResource("resources/velocity.template").getFile()
[from the "Class" object] to expand the relative location to a full
URL. This is for flexibility/portability... This approach works fine
when *not* running my app from a Jar file because the file name expands
to a simple path, something like:
"/home/mark/resources/velocity.template" whereas when run from a Jar
file, it expands to something like:
"file:/home/mark/MyApp.jar!/resources/velocity.template", and thus
mergeTemplate doesn't like this format... probably the "file:" or the
"!" throw it off.
I'm not sure how to get past this hurdle. Any ideas?
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Resource from a jar file
Posted by Nathan Bubna <nb...@gmail.com>.
Yes, but at some point, many Velocity users will "ship" an app with
"finished" templates that the customer won't modify. At that time, it
can make a lot of sense to just keep your templates in the classpath
or a non-classpath jar. Guessing from user examples and questions,
the ClasspathResourceLoader is one of the more frequently used
loaders. The JarResourceLoader is probably the least-used one.
Personally, i use the WebappResourceLoader from VelocityTools most.
On Mon, Jun 15, 2009 at 6:54 PM, Mark Fenbers<Ma...@noaa.gov> wrote:
> Amidst this dialog, the thought occurred to me that templates are made to be
> customized (or what would be the point of Velocity?), and so I realized that
> these template really don't belong in a Jar file anyway. If they were,
> people who know little about Java would have to un-jar the templates, modify
> them, and re-jar them before my app would read them. So it now makes sense
> to just read these from the file system, rather than from a jar file...
>
> Thanks, anyway, as I learned some important things in the process.
>
> Mark
>
> Nathan Bubna wrote:
>>
>> Can you put your jar in the classpath and use the
>> ClasspathResourceLoader instead of going through the file system?
>> I've never used the JarResourceLoader, and i haven't really got time
>> to dig in and see why it isn't working for you right now...
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Resource from a jar file
Posted by Mark Fenbers <Ma...@noaa.gov>.
Amidst this dialog, the thought occurred to me that templates are made
to be customized (or what would be the point of Velocity?), and so I
realized that these template really don't belong in a Jar file anyway.
If they were, people who know little about Java would have to un-jar the
templates, modify them, and re-jar them before my app would read them.
So it now makes sense to just read these from the file system, rather
than from a jar file...
Thanks, anyway, as I learned some important things in the process.
Mark
Nathan Bubna wrote:
> Can you put your jar in the classpath and use the
> ClasspathResourceLoader instead of going through the file system?
> I've never used the JarResourceLoader, and i haven't really got time
> to dig in and see why it isn't working for you right now...
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Resource from a jar file
Posted by Nathan Bubna <nb...@gmail.com>.
Can you put your jar in the classpath and use the
ClasspathResourceLoader instead of going through the file system?
I've never used the JarResourceLoader, and i haven't really got time
to dig in and see why it isn't working for you right now...
On Sun, Jun 14, 2009 at 11:21 AM, Mark Fenbers<Ma...@noaa.gov> wrote:
> Nathan Bubna wrote:
>>
>> You can configure multiple resource loaders in a lookup order.
>>
>> resource.loader=jar,file,whatever
>> jar.resource.loader.class = ...
>> file.resource.loader.class = ...
>> whatever.resource.loader.class = ...
>>
>> if Velocity can't find it via the jar one, it'll ask the file one,
>> then the whatever one, and so on until it runs out of loaders to ask.
>> Only then will it throw a ResourceNotFoundException.
>>
>
> Good to know this. My velocity.properties file now looks like this:
>
> resource.loader = jar,file
> jar.resource.loader.class =
> org.apache.velocity.runtime.resource.loader.JarResourceLoader
> jar.resource.loader.path = jar:file:templates.jar
> file.resource.path = /
>
> So now I am able to get past the Velocity.init() method without any thrown
> exceptions regardless how I'm running my app (jar, JVM, Eclipse, etc.). But
> when my app reaches the mergeTemplate() method, I get a
> ResourceNotFoundException:
>
> org.apache.velocity.exception.ResourceNotFoundException: Unable to find
> resource 'file:/home/oper/workspace/GIFTS/templates.jar!/SHEF.6hr.template'
>
> when I run from the Jar file, but do not get it if running from JVM or
> Eclipse. My /home/oper/workspace/GIFTS/templates.jar file looks like this:
>
> 7332 Mon May 04 08:14:20 EDT 2009 SHEF.6hr.template
>
> (among other entries). So despite the error, the jar file it complains
> about exists at the location given, and it also contains the template file
> that it squawks about. So I'm not sure what I'm doing wrong and why the
> resource cannot be found when it exists right where is says it is looking.
> Any ideas? I'm using v1.6.2.
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Resource from a jar file
Posted by Mark Fenbers <Ma...@noaa.gov>.
Nathan Bubna wrote:
> You can configure multiple resource loaders in a lookup order.
>
> resource.loader=jar,file,whatever
> jar.resource.loader.class = ...
> file.resource.loader.class = ...
> whatever.resource.loader.class = ...
>
> if Velocity can't find it via the jar one, it'll ask the file one,
> then the whatever one, and so on until it runs out of loaders to ask.
> Only then will it throw a ResourceNotFoundException.
>
Good to know this. My velocity.properties file now looks like this:
resource.loader = jar,file
jar.resource.loader.class =
org.apache.velocity.runtime.resource.loader.JarResourceLoader
jar.resource.loader.path = jar:file:templates.jar
file.resource.path = /
So now I am able to get past the Velocity.init() method without any
thrown exceptions regardless how I'm running my app (jar, JVM, Eclipse,
etc.). But when my app reaches the mergeTemplate() method, I get a
ResourceNotFoundException:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find
resource 'file:/home/oper/workspace/GIFTS/templates.jar!/SHEF.6hr.template'
when I run from the Jar file, but do not get it if running from JVM or
Eclipse. My /home/oper/workspace/GIFTS/templates.jar file looks like this:
7332 Mon May 04 08:14:20 EDT 2009 SHEF.6hr.template
(among other entries). So despite the error, the jar file it complains
about exists at the location given, and it also contains the template
file that it squawks about. So I'm not sure what I'm doing wrong and
why the resource cannot be found when it exists right where is says it
is looking.
Any ideas? I'm using v1.6.2.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Resource from a jar file
Posted by Nathan Bubna <nb...@gmail.com>.
On Sat, Jun 13, 2009 at 6:48 PM, Mark Fenbers<Ma...@noaa.gov> wrote:
> Nathan Bubna wrote:
>>
>> I think org.apache.velocity.runtime.resource.loader.JarResourceLoader
>> was created to help with such situations.
>>
>>
>
> OK, I read up on JarResourceLoader() and put the appropriate resource loader
> entries into the velocity.properties file, but now I have a new problem in
> that if I am not using a Jar file, it dumps a stack trace. My program can
> run from Eclipse, from a JVM, or from a jar file. I want to avoid having a
> separate velocity.properties file for each way the program can be run. Is
> there a way the velocity.properties file can be configured so that it
> doesn't matter if the program is run from a jar or from a JVM?
You can configure multiple resource loaders in a lookup order.
resource.loader=jar,file,whatever
jar.resource.loader.class = ...
file.resource.loader.class = ...
whatever.resource.loader.class = ...
if Velocity can't find it via the jar one, it'll ask the file one,
then the whatever one, and so on until it runs out of loaders to ask.
Only then will it throw a ResourceNotFoundException.
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Resource from a jar file
Posted by Mark Fenbers <Ma...@noaa.gov>.
Nathan Bubna wrote:
> I think org.apache.velocity.runtime.resource.loader.JarResourceLoader
> was created to help with such situations.
>
>
OK, I read up on JarResourceLoader() and put the appropriate resource
loader entries into the velocity.properties file, but now I have a new
problem in that if I am not using a Jar file, it dumps a stack trace.
My program can run from Eclipse, from a JVM, or from a jar file. I want
to avoid having a separate velocity.properties file for each way the
program can be run. Is there a way the velocity.properties file can be
configured so that it doesn't matter if the program is run from a jar or
from a JVM?
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Resource from a jar file
Posted by Nathan Bubna <nb...@gmail.com>.
I think org.apache.velocity.runtime.resource.loader.JarResourceLoader
was created to help with such situations.
On Sat, Jun 13, 2009 at 4:56 PM, Mark Fenbers<Ma...@noaa.gov> wrote:
> I'm having some trouble with mergeTemplate() in that it seems to be unable
> to open a template file if it is packaged in a jar file. In my Java code, I
> use getResource("resources/velocity.template").getFile() [from the "Class"
> object] to expand the relative location to a full URL. This is for
> flexibility/portability... This approach works fine when *not* running my
> app from a Jar file because the file name expands to a simple path,
> something like: "/home/mark/resources/velocity.template" whereas when run
> from a Jar file, it expands to something like:
> "file:/home/mark/MyApp.jar!/resources/velocity.template", and thus
> mergeTemplate doesn't like this format... probably the "file:" or the "!"
> throw it off.
>
> I'm not sure how to get past this hurdle. Any ideas?
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org