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