You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Peter Steele <ps...@maxiscale.com> on 2007/08/31 16:50:13 UTC

UNC paths in Velocity

Velocity does not seem to properly support UNC paths under Windows. I am running from a network drive on a Vista box and want to point Velocity to a location relative to my current dir to find my template files. I have code similar to the following:

 

        String templateDir = new File("templates");

        String absTemplateDir = templateDir.getAbsolutePath();

        Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, absTemplateDir);

        Velocity.init();

        Template myTemplate = Velocity.getTemplate("my_template.vm");

 

The getTemplate call fails saying that it cannot find the file. The init call explains why: it generates a log message similar to this:

 

INFO: FileResourceLoader : adding path '\myserver\home\peter\myproj\template'

 

The file path *should* be '\\myserver\home\peter\myproj\template', but Velocity strips one of the leading backslashes. The original string is correct that’s returned by getAbsolutePath, with four backslashes representing two real backslashes:

 

      \\\\myserver\\home\\peter\\myproj\\template

 

This is what I’d expect. This doesn’t seem to satisfy Velocity though. If I manually set the string to

 

      \\\\\\\\\\myserver\\home\\peter\\myproj\\template

 

this solves the problem. What I ended up doing though was this:

 

        String absTemplateDir = templateDir.getAbsolutePath().replace('\\', '/');

 

This also works, so obviously the problem has something to do with interpreting the leading backslash characters that represent the Windows UNC path. This seems like a bug to me…

 


No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.484 / Virus Database: 269.13.1/981 - Release Date: 8/31/2007 6:13 AM
 

Re: UNC paths in Velocity

Posted by Gonzalo Diethelm <go...@diethelm.org>.
Allow me to paste my very last reply to this list. Regardless of the '/'
vs '\' issue, Velocity does have a very quirky way of handling
backslashes... Best regards.


        I suggest you NEVER use the backslash character for path
        delimiters. As far as I know, C, C++, C# and Java all understand
        the forward slash as a path separator, with the benefits of
        portable code and not having to quote and re-quote the
        backslash. Therefore, try something like
        "//myserver/home/peter/myproj/template".


On Fri, 2007-08-31 at 07:50 -0700, Peter Steele wrote:

> Velocity does not seem to properly support UNC paths under Windows. I am running from a network drive on a Vista box and want to point Velocity to a location relative to my current dir to find my template files. I have code similar to the following:
> 
>  
> 
>         String templateDir = new File("templates");
> 
>         String absTemplateDir = templateDir.getAbsolutePath();
> 
>         Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, absTemplateDir);
> 
>         Velocity.init();
> 
>         Template myTemplate = Velocity.getTemplate("my_template.vm");
> 
>  
> 
> The getTemplate call fails saying that it cannot find the file. The init call explains why: it generates a log message similar to this:
> 
>  
> 
> INFO: FileResourceLoader : adding path '\myserver\home\peter\myproj\template'
> 
>  
> 
> The file path *should* be '\\myserver\home\peter\myproj\template', but Velocity strips one of the leading backslashes. The original string is correct that’s returned by getAbsolutePath, with four backslashes representing two real backslashes:
> 
>  
> 
>       \\\\myserver\\home\\peter\\myproj\\template
> 
>  
> 
> This is what I’d expect. This doesn’t seem to satisfy Velocity though. If I manually set the string to
> 
>  
> 
>       \\\\\\\\\\myserver\\home\\peter\\myproj\\template
> 
>  
> 
> this solves the problem. What I ended up doing though was this:
> 
>  
> 
>         String absTemplateDir = templateDir.getAbsolutePath().replace('\\', '/');
> 
>  
> 
> This also works, so obviously the problem has something to do with interpreting the leading backslash characters that represent the Windows UNC path. This seems like a bug to me…
> 
>  
> 
> 
> No virus found in this outgoing message.
> Checked by AVG Free Edition. 
> Version: 7.5.484 / Virus Database: 269.13.1/981 - Release Date: 8/31/2007 6:13 AM
>  


-- 
Gonzalo Diethelm
gonzalo.diethelm@diethelm.org