[ 
https://issues.apache.org/jira/browse/VELOCITY-932?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17361937#comment-17361937
 ] 

Svante Schubert edited comment on VELOCITY-932 at 6/11/21, 6:33 PM:
--------------------------------------------------------------------

First thought was the same reason (therefore in this issue) 
!2021-06-11_20-05-37.png!

 


was (Author: svanteschubert):
I lost half a day for the same reason... !2021-06-11_20-05-37.png!

 

> 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
>            Assignee: Claude Brisson
>            Priority: Major
>         Attachments: 2021-06-11_20-05-37.png, 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-unsubscr...@velocity.apache.org
For additional commands, e-mail: dev-h...@velocity.apache.org

Reply via email to