casid / jte

Secure and speedy templates for Java and Kotlin.
https://jte.gg
Apache License 2.0
737 stars 54 forks source link

Failed to compile with module-info.java #337

Closed JahnGeor closed 3 months ago

JahnGeor commented 4 months ago

Hi! I'm new to Java development, so I apologize in advance if the question turns out to be stupid, but I couldn't find an answer to my question on the Internet. I need to create an HTML page by template to send it for printing via JavaFX modules. However, if the module-info file is present in the project, then an error appears during the compilation of the project:

G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:2: error: package com.example.demo2 does not exist
import com.example.demo2.Body;
                        ^
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:6: error: package gg.jte.html does not exist
    public static void render(gg.jte.html.HtmlTemplateOutput jteOutput, gg.jte.html.HtmlInterceptor jteHtmlInterceptor, Body page) {
                                         ^
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:6: error: package gg.jte.html does not exist
    public static void render(gg.jte.html.HtmlTemplateOutput jteOutput, gg.jte.html.HtmlInterceptor jteHtmlInterceptor, Body page) {
                                                                                   ^
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:6: error: cannot find symbol
    public static void render(gg.jte.html.HtmlTemplateOutput jteOutput, gg.jte.html.HtmlInterceptor jteHtmlInterceptor, Body page) {
                                                                                                                        ^
  symbol:   class Body
  location: class JteexampleGenerated
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:28: error: package gg.jte.html does not exist
    public static void renderMap(gg.jte.html.HtmlTemplateOutput jteOutput, gg.jte.html.HtmlInterceptor jteHtmlInterceptor, java.util.Map<String, Object> params) {
                                            ^
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:28: error: package gg.jte.html does not exist
    public static void renderMap(gg.jte.html.HtmlTemplateOutput jteOutput, gg.jte.html.HtmlInterceptor jteHtmlInterceptor, java.util.Map<String, Object> params) {
                                                                                      ^
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:11: error: package gg.jte.runtime does not exist
            if (gg.jte.runtime.TemplateUtils.isAttributeRendered(__jte_html_attribute_0)) {
                              ^
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:29: error: cannot find symbol
        Body page = (Body)params.get("page");
        ^
  symbol:   class Body
  location: class JteexampleGenerated
G:\tmp.work\demo2\jte-classes\gg\jte\generated\ondemand\JteexampleGenerated.java:29: error: cannot find symbol
        Body page = (Body)params.get("page");
                     ^
  symbol:   class Body
  location: class JteexampleGenerated
9 errors

    at gg.jte@3.1.9/gg.jte.compiler.java.JavaClassCompiler.runCompiler(JavaClassCompiler.java:49)
    at gg.jte@3.1.9/gg.jte.compiler.java.JavaClassCompiler.compile(JavaClassCompiler.java:38)
    at gg.jte@3.1.9/gg.jte.compiler.TemplateCompiler.precompileClasses(TemplateCompiler.java:114)
    at gg.jte@3.1.9/gg.jte.compiler.TemplateCompiler.precompile(TemplateCompiler.java:94)
    at gg.jte@3.1.9/gg.jte.compiler.TemplateCompiler.load(TemplateCompiler.java:50)
    at gg.jte.runtime@3.1.9/gg.jte.TemplateEngine.lambda$resolveTemplateOnDemand$0(TemplateEngine.java:354)
    at java.base/java.util.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1916)
    at gg.jte.runtime@3.1.9/gg.jte.TemplateEngine.resolveTemplateOnDemand(TemplateEngine.java:347)
    at gg.jte.runtime@3.1.9/gg.jte.TemplateEngine.resolveTemplate(TemplateEngine.java:337)
    at gg.jte.runtime@3.1.9/gg.jte.TemplateEngine.render(TemplateEngine.java:210)
    at com.example.demo2/com.example.demo2.Main.main(Main.java:16)

There is no error without the module-info.java file and everything functions as expected. Please tell me how to modify the module-info file.java or my code to make project compiles without an error? Thanks!

Current file module-info.java is

open module com.example.demo2 {
    requires javafx.controls;
    requires javafx.fxml;
    requires gg.jte;
    requires gg.jte.runtime;
}

Project structure:

image

Executable code:

        CodeResolver codeResolver = new ResourceCodeResolver("jte");
        TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html);
        TemplateOutput output = new StringOutput();
        templateEngine.render("example.jte", new Body("Hello", "World"), output);

JDK Version - 17

JTE Version - 3.1.9

casid commented 4 months ago

Hi @JahnGeor,

I haven't used Java modules seriously so far, so have no pointer for you in the right direction.

I guess the Java Compiler would need a hint to allow access of your module, when compiling jte files.

There is this method TemplateEngine#setCompileArgs. Maybe you can specify there that this module is accessible?

JahnGeor commented 4 months ago

Hi @JahnGeor,

I haven't used Java modules seriously so far, so have no pointer for you in the right direction.

I guess the Java Compiler would need a hint to allow access of your module, when compiling jte files.

There is this method TemplateEngine#setCompileArgs. Maybe you can specify there that this module is accessible?

First of all, I would like to thank you very much for your response! After doing some research, I found the "precompilation" feature on the "gg.jte" web page and decided to use it through the "Maven" plugin. Everything is working as expected!