相关疑难解决方法(0)

在jar文件中加载速度模板

我有一个项目,我想加载一个速度模板来完成它与参数.整个应用程序打包为jar文件.我最初想到的是这样的:

VelocityEngine ve = new VelocityEngine();

   URL url = this.getClass().getResource("/templates/");

   File file = new File(url.getFile());

   ve = new VelocityEngine();
   ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
   ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, file.getAbsolutePath());
   ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");

   ve.init();

   VelocityContext context = new VelocityContext();

   if (properties != null) {
    stringfyNulls(properties);
    for (Map.Entry<String, Object> property : properties.entrySet()) {
     context.put(property.getKey(), property.getValue());
    }
   }

   final String templatePath = templateName + ".vm";
   Template template = ve.getTemplate(templatePath, "UTF-8");
   String outFileName = File.createTempFile("report", ".html").getAbsolutePath();
   BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outFileName)));

   template.merge(context, writer);

   writer.flush();
   writer.close();
Run Code Online (Sandbox Code Playgroud)

当我在eclipse中运行时,这很好用.但是,一旦我打包程序并尝试使用命令行运行它,我得到一个错误,因为找不到该文件.

我想问题就在这一行:

ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, file.getAbsolutePath()); …
Run Code Online (Sandbox Code Playgroud)

java templates velocity jar classpath

45
推荐指数
3
解决办法
4万
查看次数

标签 统计

classpath ×1

jar ×1

java ×1

templates ×1

velocity ×1