Closed venkatwilliams closed 11 months ago
Hi, from the stacktrace I assume the error happens in the TemplateEngine.createPrecompiled
branch.
When this happens with Spring Boot it usually is a classloader problem. Can you try to pass the Spring Boot classloader like this?
TemplateEngine.createPrecompiled(JTE_PRECOMPILED_FILE_PATH, ContentType.Html, getClass().getClassLoader());
With this change it is working.
TemplateEngine.createPrecompiled(JTE_PRECOMPILED_FILE_PATH, ContentType.Html, getClass().getClassLoader());
Want to know the best practice in managing "jte-classes" code.
Glad it is working!
Personally, I put the jte source files in something like src/main/jte (like we do it with Java files)
If you want a self contained jar, you can use the generate Gradle task as described here: https://github.com/casid/jte/blob/main/DOCUMENTATION.md#using-the-application-class-loader-since-120. This way the Gradle task only generates a Java file for each jte file and leaves compilation to the regular Java compiler. You can specify which package the jte templates be located in, but you don't have to. With the default config, you would just create the template engine like this: TemplateEngine templateEngine = TemplateEngine.createPrecompiled(ContentType.Html);
(I think in this case there won't be a need to specify the Spring classloader either)
Thank you very much @casid both the suggestions worked fine.
Able to include jte generated classes inside jar file using the gradle task. https://github.com/casid/jte/blob/main/DOCUMENTATION.md#using-the-application-class-loader-since-120
Handler dispatch failed: java.lang.NoClassDefFoundError: gg/jte/html/HtmlTemplateOutput] with root cause
java.lang.ClassNotFoundException: gg.jte.html.HtmlTemplateOutput at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:592) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[na:na] at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na] at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402) ~[na:na] at java.base/java.lang.Class.getDeclaredMethods(Class.java:2504) ~[na:na] at gg.jte.runtime.Template.findRenderMethods(Template.java:69) ~[jte-runtime-3.1.0.jar!/:na] at gg.jte.runtime.Template.(Template.java:26) ~[jte-runtime-3.1.0.jar!/:na]
at gg.jte.runtime.TemplateLoader.load(TemplateLoader.java:26) ~[jte-runtime-3.1.0.jar!/:na]
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
at gg.jte.TemplateEngine.resolveTemplate(TemplateEngine.java:339) ~[jte-runtime-3.1.0.jar!/:na]
at gg.jte.TemplateEngine.render(TemplateEngine.java:228) ~[jte-runtime-3.1.0.jar!/:na]
private static Path JTE_FILE_PATH = Path.of("src", "main", "resources", "jte"); private static Path JTE_PRECOMPILED_FILE_PATH = Path.of("jte-classes");
I can see the dependent libraries in the jar file.
inflated: META-INF/build-info.properties created: BOOT-INF/classes/jte/ inflated: BOOT-INF/classes/jte/hello.jte inflated: BOOT-INF/classes/application.properties extracted: BOOT-INF/lib/jte-extension-api-3.1.0.jar extracted: BOOT-INF/lib/jte-runtime-3.1.0.jar extracted: BOOT-INF/lib/jte-3.1.0.jar
build.gradle file