'Velocity template cannot be found in Windows 10

My laptop just got upgraded to windows 10 from windows 7, and a piece of code stopped working. A large application using velocity templates which used to work on windows 7 just fine now cannot find the template files.

The templates are kept at the path WebContent\WEB-INF\config\templates under the project directory. An EngineInitializer class is used to load them. The code for the class is as follows:

private static Logger logger = Logger.getLogger(EngineInitializer.class);

private static String RELATIVE_PATH_FOR_TEMPLATES = "/WEB-INF/config/templates";
    if(logger.isDebugEnabled())
        logger.debug("About to initialize the Velocity Engine");
    Properties p = new Properties();
    String absolutePath=new File(Thread.currentThread().getContextClassLoader().getResource("").getFile()).getParentFile().getParentFile().getPath();//this goes to webapps directory
    //configure the velocity logger to use the default logging
    p.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,"org.apache.velocity.runtime.log.Log4JLogChute");
    p.put("runtime.log.logsystem.log4j.logger", "defaultLog");
    p.put("file.resource.loader.path", absolutePath + RELATIVE_PATH_FOR_TEMPLATES);
    p.put("file.resource.loader.cache", "true");
    p.put("file.resource.loader.modificationCheckInterval", "-1");
    p.put("parser.pool.size", "30");

    Velocity.init(p);
    if(logger.isInfoEnabled())
        logger.info("The velocity engine is now initialized..");

The following lines in the applicationBeans.xml file initializes the engine:

<!-- initialize the velocity engine before the listener thread starts -->
<bean id="engineInitializer" class="com.file.myprogram.template.processor.EngineInitializer"
    init-method="initializeEngine" />

At the beginning, the log is getting printed. Inside the individual classes, the templates are called using Velocity.getTemplate() method. This is now returning a org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'MediationZone.vm' error. Nothing other than the underlying OS has been changed. This code runs fine on an RHEL server as a web app. Code has been downloaded from subversion and run on the windows 10 laptop using eclipse v4.9.

What is going wrong here?



Solution 1:[1]

I had the same issue and nothing worked (file:/ prefix variants, C: or c:).

Finally I tried it with some relative path (like used in the production environment) and voilĂ , it worked.

Figured out current run dir using System.getProperty("user.dir"):

vel.getTemplate( "foo/bar/some.vm", UTF_8.name() )

 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andreas Covidiot