Loading velocity template inside a jar file

前端 未结 5 902
耶瑟儿~
耶瑟儿~ 2020-12-23 19:48

I have a project where I want to load a velocity template to complete it with parameters. The whole application is packaged as a jar file. What I initially thought of doing

5条回答
  •  失恋的感觉
    2020-12-23 20:30

    Unless JAR is exploded, you can't read the resource in JAR as file. Use an input stream.

    See following code snippets,

        InputStream input = classLoader.getResourceAsStream(fileName);
        if (input == null) {
            throw new ConfigurationException("Template file " +
                    fileName + " doesn't exist");           
        }
    
        InputStreamReader reader = new InputStreamReader(input);            
            Writer writer = null;
    
            try {
                writer = new OutputStreamWriter(output);        
    
                // Merge template
                if (!engine.evaluate(context, writer, fileName, reader)) 
                    ......
    

提交回复
热议问题