You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Pierre-François Lemosquet <pf...@wokup.com> on 2001/06/20 11:57:44 UTC

Template in jar

Hi!

I use a template located in c:\temp\foo.tpl

 java.util.Properties oProperties = new Properties();
 oProperties.setProperty("file.resource.loader.path", "c:\\temp");
 Velocity.init(oProperties);
 Template oTemplate = Velocity.getTemplate("foo.tpl");

But, how to do to use a template which is located in a jar  ?

--
Pierre-François Lemosquet

Product team
E-Mail: pf.lemosquet@wokup.com

Wokup! Get your dotcom anywere...
http://www.wokup.com



Re: Template in jar

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Simon Christian wrote:
> 
> Hi,
> 
> You need to use the JarResourceLoader instead of the FileResourceLoader.
> This should be simply a matter of changing a couple of lines, to
> something like:
> 
> java.util.Properties oProperties = new Properties();
> oProperties.setProperty("resource.loader", "jar");
> oProperties.setProperty("jar.resource.loader.path", "c:\\temp\foo.jar");
> Velocity.init(oProperties);
> Template oTemplate = Velocity.getTemplate("foo.tpl");
> 
> Check the Resource Management section in the developer-guide for more
> detail.

You also need to sepecify the Class of the resource loader 'jar', so add
the line :

oProperties.setProperty("jar.resource.loader.class",
 "org.apache.velocity.runtime.resource.loader.JarResourceLoader");

Also, if in a web environment, use the classpath resource loader, toss
the jar (or jars) into WEB-INF/lib of the webapp, and then your
configuration simplifies to

Properties p = new Properties();
p.setProperty("resource.loader","class");
oProperties.setProperty("class.resource.loader.class",
 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(p);

and you don't even have to list the jars - they will get picked up
automatically.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: template load problem with evaluate()in Vel 1.1

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Dan Finkelstein wrote:
> 
> Cool!   I can't believe how fast you got to the bottom of this...

And I can't believe how stupid my bug is... :)

geir

> Dan
> 
> At 03:36 PM 6/20/01 -0400, you wrote:
> >Dan Finkelstein wrote:
> > >
> > > Hi Geir--
> > > It is bizzare!  The log didn't seem too informative, but here it is:
> > > Thanks, Dan
> > >
> > > Wed Jun 20 11:35:45 PDT 2001 AvalonLogSystem initialized using logfile
> > > C:\t\velocity.log
> > > Wed Jun 20 11:35:45 PDT 2001   [info]   [info] Default Properties File:
> >
> >[SNIP]
> >
> >I know what it is.  My bad.  Will fix.  Sorry...
> >
> >This prollie means a 1.1.1 release sequence. Any other requests? :)
> >
> >geir
> >

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: template load problem with evaluate()in Vel 1.1

Posted by Dan Finkelstein <da...@emind.com>.
Cool!   I can't believe how fast you got to the bottom of this...
Dan

At 03:36 PM 6/20/01 -0400, you wrote:
>Dan Finkelstein wrote:
> >
> > Hi Geir--
> > It is bizzare!  The log didn't seem too informative, but here it is:
> > Thanks, Dan
> >
> > Wed Jun 20 11:35:45 PDT 2001 AvalonLogSystem initialized using logfile
> > C:\t\velocity.log
> > Wed Jun 20 11:35:45 PDT 2001   [info]   [info] Default Properties File:
>
>[SNIP]
>
>I know what it is.  My bad.  Will fix.  Sorry...
>
>This prollie means a 1.1.1 release sequence. Any other requests? :)
>
>geir
>
>--
>Geir Magnusson Jr.                           geirm@optonline.net
>System and Software Consulting
>Developing for the web?  See http://jakarta.apache.org/velocity/
>You have a genius for suggesting things I've come a cropper with!

Dan Finkelstein, Senior Architect, Instructional Tools & Technology
eMind, 1250 Addison St, Suite #210, Berkeley,CA,94702-1700
510/486-2740, dan.finkelstein@eMind.com

"Changing the face of the on-line medium" (again!)




Re: template load problem with evaluate()in Vel 1.1

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Dan Finkelstein wrote:
> 
> Hi Geir--
> It is bizzare!  The log didn't seem too informative, but here it is:
> Thanks, Dan
> 
> Wed Jun 20 11:35:45 PDT 2001 AvalonLogSystem initialized using logfile
> C:\t\velocity.log
> Wed Jun 20 11:35:45 PDT 2001   [info]   [info] Default Properties File:

[SNIP]

I know what it is.  My bad.  Will fix.  Sorry...

This prollie means a 1.1.1 release sequence. Any other requests? :)

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: template load problem with evaluate()in Vel 1.1

Posted by Dan Finkelstein <da...@emind.com>.
Hi Geir--
It is bizzare!  The log didn't seem too informative, but here it is:
Thanks, Dan

Wed Jun 20 11:35:45 PDT 2001 AvalonLogSystem initialized using logfile 
C:\t\velocity.log
Wed Jun 20 11:35:45 PDT 2001   [info]   [info] Default Properties File: 
org\apache\velocity\runtime\defaults\velocity.properties
Wed Jun 20 11:35:45 PDT 2001   [info] Resource Loader Instantiated: 
org.apache.velocity.runtime.resource.loader.FileResourceLoader
Wed Jun 20 11:35:45 PDT 2001   [info] FileResourceLoader : initialization 
starting.
Wed Jun 20 11:35:45 PDT 2001   [info] FileResourceLoader : adding path '/'
Wed Jun 20 11:35:45 PDT 2001   [info] FileResourceLoader : initialization 
complete.
Wed Jun 20 11:35:45 PDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Literal
Wed Jun 20 11:35:45 PDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Macro
Wed Jun 20 11:35:45 PDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Parse
Wed Jun 20 11:35:45 PDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Include
Wed Jun 20 11:35:45 PDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Foreach
Wed Jun 20 11:35:46 PDT 2001   [info] Created: 20 parsers.
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : initialization starting.
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : adding VMs from VM 
library template : VM_global_library.vm
Wed Jun 20 11:35:46 PDT 2001  [error] ResourceManager : unable to find 
resource 'VM_global_library.vm' in any resource loader.
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : error using  VM library 
template VM_global_library.vm : 
org.apache.velocity.exception.ResourceNotFoundException: Unable to find 
resource 'VM_global_library.vm'
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : allowInline = true : 
VMs can be defined inline in templates
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : allowInlineToOverride = 
false : VMs defined inline may NOT replace previous VM definitions
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : allowInlineLocal = 
false : VMs defined inline will be  global in scope if allowed.
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : messages on  : VM 
system will output logging messages
Wed Jun 20 11:35:46 PDT 2001   [info] Velocimacro : initialization complete.
Wed Jun 20 11:35:46 PDT 2001   [info] Velocity successfully started.
Wed Jun 20 11:35:46 PDT 2001  [error] #include : cannot find t/test.vm 
template!




At 03:02 PM 6/20/01 -0400, you wrote:
>Dan Finkelstein wrote:
> >
> > Hi --
> >
> > The resource loader is unable to find #include files which Velocity 1.1
> > when running with Velocity.evaluate().  It works ok if I were to use
> > template.merge().  And it also works ok with Velocity 1.0.
> >
>
>Hm.  That's not good.
>
> > But with 1.1, the output is something like
> >
> >         java Example
> >         calling Velocity.evaluate()...
> >
> >         Velocity is great!
> >
> >         <!-- include error : error with arg 0 please see log. see error 
> log -->
> >
>
>What does the log say?
>
> > when I run the test program shown below.  The test program is run from a /t
> > directory.  As you can see, the resource file loader path is set to the
> > root directory.  There is a withEvaluate boolean which can be changed to
> > run the program either with evaluate() or with merge().
> >
> > It's very strange that it works in most modes...
>
>Bizarre.  Will investigate...
>
>geir
>
>--
>Geir Magnusson Jr.                           geirm@optonline.net
>System and Software Consulting
>Developing for the web?  See http://jakarta.apache.org/velocity/
>You have a genius for suggesting things I've come a cropper with!

Dan Finkelstein, Senior Architect, Instructional Tools & Technology
eMind, 1250 Addison St, Suite #210, Berkeley,CA,94702-1700
510/486-2740, dan.finkelstein@eMind.com

"Changing the face of the on-line medium" (again!)




Re: template load problem with evaluate()in Vel 1.1

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Dan Finkelstein wrote:
> 
> Hi --
> 
> The resource loader is unable to find #include files which Velocity 1.1
> when running with Velocity.evaluate().  It works ok if I were to use
> template.merge().  And it also works ok with Velocity 1.0.
> 

Hm.  That's not good.

> But with 1.1, the output is something like
> 
>         java Example
>         calling Velocity.evaluate()...
> 
>         Velocity is great!
> 
>         <!-- include error : error with arg 0 please see log. see error log -->
> 

What does the log say?

> when I run the test program shown below.  The test program is run from a /t
> directory.  As you can see, the resource file loader path is set to the
> root directory.  There is a withEvaluate boolean which can be changed to
> run the program either with evaluate() or with merge().
> 
> It's very strange that it works in most modes...

Bizarre.  Will investigate...

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

template load problem with evaluate()in Vel 1.1

Posted by Dan Finkelstein <da...@emind.com>.
Hi --

The resource loader is unable to find #include files which Velocity 1.1 
when running with Velocity.evaluate().  It works ok if I were to use 
template.merge().  And it also works ok with Velocity 1.0.

But with 1.1, the output is something like

	java Example
	calling Velocity.evaluate()...

	Velocity is great!

	<!-- include error : error with arg 0 please see log. see error log -->

when I run the test program shown below.  The test program is run from a /t 
directory.  As you can see, the resource file loader path is set to the 
root directory.  There is a withEvaluate boolean which can be changed to 
run the program either with evaluate() or with merge().

It's very strange that it works in most modes...

Dan


 >>>> THIS IS example.java

import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import java.io.*;
import java.util.ArrayList;


public class Example
{
     public Example()
     {
		final boolean withEvaluate = false;

         try
         {
			Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "/");

			Velocity.init();

             VelocityContext context = new VelocityContext();

			BufferedWriter writer = writer = new BufferedWriter(
				new OutputStreamWriter(System.out));


			if(withEvaluate) {
				System.out.println("calling Velocity.evaluate()...");

				String templateFile = "example.vm";
				FileInputStream fis = new FileInputStream(templateFile);

				if (fis == null)
					throw new Exception ("Cannot find template: " + templateFile);
		
				Velocity.evaluate(context, writer, templateFile, fis);
				fis.close();

			} else {
				System.out.println("calling template.merge()...");
				String templateFile = "t/example.vm";

	            Template template =  null;

	            try
	            {
	                template = Velocity.getTemplate(templateFile);
	            }
	           catch( ResourceNotFoundException rnfe )
	            {
	                System.out.println("Example : error : cannot find template 
" + templateFile );
	            }
	            catch( ParseErrorException pee )
	            {
	                System.out.println("Example : Syntax error in template " + 
templateFile + ":" + pee );
	            }

	            if ( template != null)
	                template.merge(context, writer);

			}

             writer.flush();
             writer.close();
         }
         catch( Exception e )
         {
             System.out.println(e);
         }
     }


     public static void main(String[] args)
     {
         Example t = new Example();
     }
}

 >>>>> THIS IS test.vm

FROM \T DIRECTORY ===> HELLO FROM TEST.VM

 >>>>>> THIS IS example.vm

#set( $this = "Velocity")

$this is great!

#include ("t/test.vm")


Dan Finkelstein, Senior Architect, Instructional Tools & Technology
eMind, 1250 Addison St, Suite #210, Berkeley,CA,94702-1700
510/486-2740, dan.finkelstein@eMind.com

"Changing the face of the on-line medium" (again!)




Re: Template in jar

Posted by Simon Christian <si...@cpd.co.uk>.
Pierre did you see the response from Geir? Seems I missed something out,
specifically:

	oProperties.setProperty("jar.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.JarResourceLoader");

check his message..

- simon


Pierre-François Lemosquet wrote:
> 
> Thank you Simon,
> 
> OK for that
>                 java.util.Properties oProperties = new Properties();
>                 oProperties.setProperty("resource.loader", "jar");
>                 oProperties.setProperty("jar.resource.loader.path",
> "c:\\Wokup\\lib\\foo.jar");
>                 Velocity.init(oProperties);
> 
> But i have this error :
> 
> Unable to find resource 'foo.tpl'
>

Re: Template in jar

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Because you didn't tell velocity what Class to use.  Read my followup
post...

geir


Pierre-François Lemosquet wrote:
> 
> Thank you Simon,
> 
> OK for that
>                 java.util.Properties oProperties = new Properties();
>                 oProperties.setProperty("resource.loader", "jar");
>                 oProperties.setProperty("jar.resource.loader.path",
> "c:\\Wokup\\lib\\foo.jar");
>                 Velocity.init(oProperties);
> 
> But i have this error :
> 
> Unable to find resource 'foo.tpl'
> 
> when i execute the following code
> 
>           try
>          {
>                 oTemplate = Velocity.getTemplate("foo.tpl");
>                 oTemplate.merge(getVelocityContext(), oBufferedWriter);
>                 oBufferedWriter.flush();
>            }
>            catch(Exception oE)
>            {
>                 throw new InternalWokException(oE.getMessage());
>            }
> 
> --
> Pierre-François Lemosquet
> 
> Simon Christian wrote:
> 
> > Hi,
> >
> > You need to use the JarResourceLoader instead of the FileResourceLoader.
> > This should be simply a matter of changing a couple of lines, to
> > something like:
> >
> > java.util.Properties oProperties = new Properties();
> > oProperties.setProperty("resource.loader", "jar");
> > oProperties.setProperty("jar.resource.loader.path", "c:\\temp\foo.jar");
> > Velocity.init(oProperties);
> > Template oTemplate = Velocity.getTemplate("foo.tpl");
> >
> > Check the Resource Management section in the developer-guide for more
> > detail.
> >
> > - simon
> >
> > Pierre-François Lemosquet wrote:
> > >
> > > Hi!
> > >
> > > I use a template located in c:\temp\foo.tpl
> > >
> > >  java.util.Properties oProperties = new Properties();
> > >  oProperties.setProperty("file.resource.loader.path", "c:\\temp");
> > >  Velocity.init(oProperties);
> > >  Template oTemplate = Velocity.getTemplate("foo.tpl");
> > >
> > > But, how to do to use a template which is located in a jar  ?
> > >

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: Template in jar

Posted by Pierre-François Lemosquet <pf...@wokup.com>.
Thank you Simon,

OK for that
                java.util.Properties oProperties = new Properties();
                oProperties.setProperty("resource.loader", "jar");
                oProperties.setProperty("jar.resource.loader.path",
"c:\\Wokup\\lib\\foo.jar");
                Velocity.init(oProperties);


But i have this error :

Unable to find resource 'foo.tpl'

when i execute the following code

          try
         {
                oTemplate = Velocity.getTemplate("foo.tpl");
                oTemplate.merge(getVelocityContext(), oBufferedWriter);
                oBufferedWriter.flush();
           }
           catch(Exception oE)
           {
                throw new InternalWokException(oE.getMessage());
           }


--
Pierre-François Lemosquet


Simon Christian wrote:

> Hi,
>
> You need to use the JarResourceLoader instead of the FileResourceLoader.
> This should be simply a matter of changing a couple of lines, to
> something like:
>
> java.util.Properties oProperties = new Properties();
> oProperties.setProperty("resource.loader", "jar");
> oProperties.setProperty("jar.resource.loader.path", "c:\\temp\foo.jar");
> Velocity.init(oProperties);
> Template oTemplate = Velocity.getTemplate("foo.tpl");
>
> Check the Resource Management section in the developer-guide for more
> detail.
>
> - simon
>
> Pierre-François Lemosquet wrote:
> >
> > Hi!
> >
> > I use a template located in c:\temp\foo.tpl
> >
> >  java.util.Properties oProperties = new Properties();
> >  oProperties.setProperty("file.resource.loader.path", "c:\\temp");
> >  Velocity.init(oProperties);
> >  Template oTemplate = Velocity.getTemplate("foo.tpl");
> >
> > But, how to do to use a template which is located in a jar  ?
> >






Re: Template in jar

Posted by Simon Christian <si...@cpd.co.uk>.
Hi,

You need to use the JarResourceLoader instead of the FileResourceLoader.
This should be simply a matter of changing a couple of lines, to
something like:

java.util.Properties oProperties = new Properties();
oProperties.setProperty("resource.loader", "jar");
oProperties.setProperty("jar.resource.loader.path", "c:\\temp\foo.jar");
Velocity.init(oProperties);
Template oTemplate = Velocity.getTemplate("foo.tpl");


Check the Resource Management section in the developer-guide for more
detail.

- simon



Pierre-François Lemosquet wrote:
> 
> Hi!
> 
> I use a template located in c:\temp\foo.tpl
> 
>  java.util.Properties oProperties = new Properties();
>  oProperties.setProperty("file.resource.loader.path", "c:\\temp");
>  Velocity.init(oProperties);
>  Template oTemplate = Velocity.getTemplate("foo.tpl");
> 
> But, how to do to use a template which is located in a jar  ?
>