Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java apache velocity资源未找到异常,即使模板文件位于同一目录中_Java_Templates_Velocity - Fatal编程技术网

Java apache velocity资源未找到异常,即使模板文件位于同一目录中

Java apache velocity资源未找到异常,即使模板文件位于同一目录中,java,templates,velocity,Java,Templates,Velocity,我已经学习了velocity作为模板引擎,并正在我的代码片段中进行尝试 package myproj.templating.velocity; import java.io.StringWriter; import java.util.Properties; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Veloci

我已经学习了velocity作为模板引擎,并正在我的代码片段中进行尝试

package myproj.templating.velocity;

import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

public class velocityTest {

    public static void main (String args[]) {

        VelocityEngine ve = new VelocityEngine();
        ve.init();

        VelocityContext context = new VelocityContext();

        context.put( "name", new String("Velocity") );

        Template template = null;

        try
        {
           template = ve.getTemplate("mytemplate.vm");
        }
        catch (ResourceNotFoundException rne) {
            rne.printStackTrace();
        }
        catch ( ParseErrorException pee )

        {
          // syntax error: problem parsing the template
            pee.printStackTrace();
        }
        catch( MethodInvocationException mie )
        {
          // something invoked in the template
          // threw an exception
            mie.printStackTrace();
        }
        catch( Exception e )
        { e.printStackTrace();}

        StringWriter sw = new StringWriter();

        template.merge( context, sw );

        System.out.println("result= " + sw.toString());
    }
}

mytemplate.vm文件实际位于与此代码相同的目录中,我已多次检查拼写。但我仍然得到ResourceNotFound异常。有什么不对劲吗?我尝试了stackflow的建议,添加filepath和其他内容,但仍然得到相同的异常

接下来的设置成功了

Properties p = new Properties();
 p.setProperty("resource.loader","file");
 p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
 p.setProperty("file.resource.loader.path","/actual/full/path/here");           
 p.setProperty("file.resource.loader.cache", "false");
 p.setProperty("file.resource.loader.modificationCheckInterval", "0");

ve.init(p);
尝试解决方案。没用。