文件目录结构:
源程序很简单,就是在网上看到的HelloVelocity如下:
vm为: Welcome $name to wuweishe.blogchina.com!
java文件为: import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class HelloVelocity
{
public static void main(String[] args) throws Exception
{
/* first, get and initialize an engine */
VelocityEngine engine = new VelocityEngine();
engine.init();
/* next, get the Template */
Template template = engine.getTemplate( "hellovelocity.vm" );
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "World");
/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
template.merge( context, writer );
/* display the results */
System.out.println( writer.toString() );
}
}
编译能通过但运行时提示:
Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/commons/collections/ExtendedProperties
at org.apache.velocity.runtime.RuntimeInstance.<init><RuntimeInstance.java:160>
at org.apache.velocity.app.VelocityEngine.<init><VelocityEngine.java:71>
at HelloVelocity.main(HelloVelocity.java:13)
可以看出,是org.apache.commons.collections.ExtendedProperties没有加载进去啊,
这个类应该在velocity-dep-1.5.jar下面,所以你肯定没有把velocity-dep-1.5.jar加载到你的类中.