0

The following line of code:

SoyFileSet sfs = sfsBuilder.add(this.getClass().getResource("templates/mail.soy")).build();

works when run outside of a Jar but fails with a null pointer exception. Similar posts indicate that I should try getResourceAsStream but the add method wants a URL. So I think I have to convert the Stream to a URL but it is not clear to me exactly how to proceed.

Jeff
  • 1,513
  • 4
  • 18
  • 34

3 Answers3

0

sfsBuilder.add() also allows you to add content as CharSequence. So, you can read the file yourself and add it to the builder.

Something like:

SoyFileSet sfs = SoyFileSet.builder().add(IOUtils.toString(this.getClass().getResourceAsStream("templates/mail.soy")), "file/path/for/messages").build();

IOUtils is from apache's commons-io project

Paulo Santos
  • 661
  • 7
  • 11
  • This does not seem to compile. – Jeff Oct 02 '15 at 19:53
  • Response updated. That method also requires a filePath for messages. https://closure-templates.googlecode.com/svn/trunk/javadoc-lite/com/google/template/soy/SoyFileSet.Builder.html – Paulo Santos Oct 02 '15 at 20:02
  • what are the messages? is it output directory or should it be non-empty when I start? – Jeff Oct 02 '15 at 20:04
  • This is still failing. I have tried templates with a leading slash and without but am getting an Unknown source error which I think has to do with IOUtils not finding mail.soy. – Jeff Oct 02 '15 at 20:26
0

By the code you have posted, I guess templates/mail.soy is a file which belongs to your project, so it should be properly packed into the jar. However, the path should begin with a slash: /templates/mail.soy will match the following jar structure:

META-INF/
templates/
    mail.soy
mypackage/
    MyProgram.class
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Little Santi
  • 8,563
  • 2
  • 18
  • 46
0

This finally worked as follows:

{SoyFileSet sfs = sfsBuilder.add(this.getClass().getResource("/resources/templates/email.soy")).build();}

Not sure if this matters but in Eclipse the Runnable Jar was created using Package Required Libraries...

Jeff
  • 1,513
  • 4
  • 18
  • 34