You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by "Tim Colson (Jira)" <ji...@apache.org> on 2021/02/12 19:00:00 UTC
[jira] [Commented] (VELOCITY-932) Resource not found when executing
jar file, works fine in IDE
[ https://issues.apache.org/jira/browse/VELOCITY-932?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17283916#comment-17283916 ]
Tim Colson commented on VELOCITY-932:
-------------------------------------
Browsing old issues. Curious if Mr. [~michael-o] meant the problem is in the user-code wrt the template location, which would mean the user can fix it, and this isn't a bug?
> Resource not found when executing jar file, works fine in IDE
> -------------------------------------------------------------
>
> Key: VELOCITY-932
> URL: https://issues.apache.org/jira/browse/VELOCITY-932
> Project: Velocity
> Issue Type: Bug
> Components: Engine
> Affects Versions: 1.7.x, 2.2
> Environment: local
> Reporter: ecstasy
> Priority: Major
> Attachments: TestVelocity_src.zip
>
>
> Hello, I have a simple maven project with 1 class file. The project works fine in eclipse but when creating a jar out of it and executing it, it runs into error of ResourceNotFound
>
> {code:java}
> SEVERE: ResourceManager : unable to find resource '\templates\welcomeLetter.vm' in any resource loader.
> Exception in thread "main" java.lang.ExceptionInInitializerError
> Caused by: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource '\templates\welcomeLetter.vm'
> at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
> at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
> at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
> at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
> at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)
> at net.rajanpanchal.handlers.WelcomeLetterGeneratorHandler.<clinit>(WelcomeLetterGeneratorHandler.java:48)
> {code}
>
> Class file:
> {code:java}
> public class WelcomeLetterGeneratorHandler implements RequestHandler<Map<String, String>, String> {
> String fileObjKeyName = "welcome_letter.txt";
> static VelocityContext vContext;
> static Template t;
>
> static {
> // Create a new Velocity Engine
> VelocityEngine velocityEngine = new VelocityEngine(); // Set properties that allow reading vm file from classpath.
> Properties p = new Properties();
> velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file,class,classpath");
> velocityEngine.setProperty("class.resource.loader.class",
> "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
> velocityEngine.setProperty("file.resource.loader.path", "classpath");
> try {
> velocityEngine.init(p);
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> // read the template from resources folder
> System.out.println("Current dir:"+System.getProperty("user.dir"));
> try {
> t = velocityEngine.getTemplate("\\templates\\welcomeLetter.vm");
> } catch (ResourceNotFoundException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (ParseErrorException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } vContext = new VelocityContext();
> } public String handleRequest(Map<String, String> event, Context context) {
> String response = new String("200 OK");
> try {
> // Add data to velocity context
> vContext.put("name", "Joe");
> File f = new File(fileObjKeyName); FileWriter writer = new FileWriter(f);
> // merge template and Data
> t.merge(vContext, writer);
> writer.flush();
> writer.close(); } catch (Exception ex) {
> throw new RuntimeException(ex);
> } return response;
> }
>
> public static void main(String[] args) {
> // TODO Auto-generated method stub
> WelcomeLetterGeneratorHandler handler = new WelcomeLetterGeneratorHandler();
> Context context = null;
> handler.handleRequest(null, context);
>
> /* first, we init the runtime engine. Defaults are fine. */ }
> }
> {code}
> Attaching Source maven project.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org