遇到的问题:
一、运行官方例子中的./example.sh出错。出现以下错误
shaoyongyang@Hack:~/Desktop/velocity/velocity-1.7/examples/app_example1$ ./example.sh
Running Example with input file 'example.vm'
错误: 找不到或无法加载主类 Example
尝试解决./example.sh脚本问题:
for i in ../../bin/*.jar
do
_VELCP=$_VELCP:"$i"
done
for i in ../../build/lib/*.jar
do
_VELCP=$_VELCP:"$i"
done
通过上面代码可以看出环境变量需要jar包。或者目录等。 然后去那个目录寻找。
shaoyongyang@Hack:~/Desktop/velocity/velocity-1.7/build$ ls
build.properties download.xml lib xsl
build.xml findbugs-exclude.xml testcases.xml
shaoyongyang@Hack:~/Desktop/velocity/velocity-1.7/build$
可以看到需要ant下载。结果运行ant。下载出错,连接不到服务器。
百思不得其解的时候。看到大侠用maven解决了jar包依赖的问题。
1、新建maven工程
2、拷贝Example.java到工程目录
3、加入pom.xml依赖
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
4、加入example.vm和velocity.proerties到src目录
5、寻找到main方法 修改为 Example t = new Example("/src/example.vm");
最后运行!!! 出现了!
Velocity is great!
ArrayList element 1 is great!
ArrayList element 2 is great!
ArrayList element 3 is great!
ArrayList element 4 is great!
The condition is true!
最后:虽然不知道为什么解决了,但是总算解决了。
附图一张: