Resources.getResource("my-template.html") -- where should my-template.html be??

Answered

Hello, I have switched from GAE to Glassfish...

Stuck on one thing...Where should my-template.html be??

I have tried many things...setting a folder to be root resource folder and adding a directory name to the template.
I have just been flailing...

Here is code I am trying to get working.

Jinjava jinjava = new Jinjava();
Map<String, Object> context = Maps.newHashMap();
context.put("name", "Jared");
String template = Resources.toString(Resources.getResource("my-template.html"), Charsets.UTF_8);
String renderedTemplate = jinjava.render(template, context);

Thank you,
MIke

 

1
11 comments

Placing the file into the resources directory root should work.

Verify that it appears in the correct classpath location, check the artifact configuration to ensure that it includes the compiler output. Check the compiler output (target) directory to ensure that it contains this resource.

If the issue remains, please provide a sample project illustrating the problem and the exact steps to reproduce.

0
Avatar
Permanently deleted user

Thank you so much for your quick reply.

Attached is a screenshot showing the project. 

Resources is marked as a resources root. 

In addition here are the resources patterns:
!?*.form
!?*.class
!?*.groovy
!?*.scala
!?*.flex
!?*.kt
!?*.clj
!?*.aj
resources:*

Thanks for your help.

-Mike

0

The patterns configuration affects source roots, files from the resource roots are always copied.

Check that this resource is present inside the artifact as well.

0
Avatar
Permanently deleted user

Boy this can't be this hard.

Is there anybody who has done Resources.getRsource("anything) with glassfish?

I must need to configure something...I have spent hours on this now.

Could somebody give me a checklist assuming I don't know anything?

Thank you,

 Mike

 

0

Don't think so, it may be easier for others to help you if you share the sample project.

0
Avatar
Permanently deleted user

How do I export the whole project into a zip file?

 

0

Just zip the project root folder with your favorite file manager.

0
Avatar
Permanently deleted user

How do I attach it to the conversation?

0

This forum doesn't support non-image attaches, you either share it via Dropbox, Google Drive. OneDrive, any other file sharing service, or upload it to our server: https://intellij-support.jetbrains.com/hc/articles/206869619 or submit a ticket to support: https://intellij-support.jetbrains.com/hc/requests/new.

0
Avatar
Permanently deleted user

Ok, I put the whole project on onto github!

https://github.com/chaski-kpu/TVWS2

I appreciate any help.

-Mike

0

The problem seems to be caused by the Guava library Resources.getResource() method. It returns wrong classloader which doesn't have access to the resources when the app runs on Glassfish. You can find a couple of related reports:

http://stackoverflow.com/questions/42536304/java-guava-resources-getresource-doesnt-work-from-far-jar
http://stackoverflow.com/questions/8678446/difference-between-guava-resources-getresourcebddconf-xml-and-properties-g

The solution would be to use getClass().getResource() instead:

String template = Resources.toString(getClass().getResource("mytemplate.html"), Charsets.UTF_8);

Another problem with your sample is that you have configured the resources directory as Test Resources instead of Resources, it should look like this:

1

Please sign in to leave a comment.