开发者

Load a resource in Jar

开发者 https://www.devze.com 2023-02-19 11:37 出处:网络
Below is structure of my Jar file root - template.ftl - org.project.myproject.App.java Inside App.java, I have a line of code that expects me to specify the directory for loading the template.ftl

Below is structure of my Jar file

root
 - template.ftl
 - org.project.myproject.App.java

Inside App.java, I have a line of code that expects me to specify the directory for loading the template.ftl. Something like:

Line#1: cfg.setDirectoryForTemplateLoading("java.io.File object that represents /directory/for/storing/template/files");

and the next line of code, read the template file

Line#2: Template temp = cfg.getTemplate("template.ftl");

My problem is that I'm not able to specify the path of directory from where the file will be loaded. The template file to load is available in root of the Jar. When I write,

cfg.setDirectoryForTemplateLo开发者_开发技巧ading(new File("."));

It says, template.ftl not found.

What should be apprpriate code that can set the template directory correctly at Line#1 above?


I think this isn't working for you because you're trying to load a file that doesn't exist as a file but is inside your jar. To read your template as an input stream from within the jar file you could do:

this.getClass().getClassLoader().getResourceAsStream("template.ftl");

I think this should help get you onto the right track.


Line#2: Template temp = cfg.getTemplate("/template.ftl");
0

精彩评论

暂无评论...
验证码 换一张
取 消