Uploaded image for project: 'Velocity'
  1. Velocity
  2. VELOCITY-932

Resource not found when executing jar file, works fine in IDE

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Bug
    • Affects Version/s: 1.7.x, 2.2
    • Fix Version/s: None
    • Component/s: Engine
    • Labels:
      None
    • Environment:
      local

      Description

      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

       

       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)
      

       

      Class file:

      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. */        	}
      }
      

      Attaching Source maven project.

        Attachments

        1. TestVelocity_src.zip
          9 kB
          ecstasy
        2. 2021-06-11_20-05-37.png
          27 kB
          Svante Schubert

          Activity

            People

            • Assignee:
              cbrisson Claude Brisson
              Reporter:
              ecstasy ecstasy
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: