98 Star 575 Fork 123

闲.大赋(李家智) / Beetl

 / 详情

类不存在

待办的
创建于  
2020-11-26 15:25

输入图片说明
java.lang.NoClassDefFoundError: Could not initialize class com.example.demo.BeetlTemplateUtils
jdk11+spring boot 2.4.0,项目打包后运行,报类不存在

评论 (8)

乀柿子不軟丷 创建了任务
乀柿子不軟丷 关联仓库设置为闲.大赋/Beetl
展开全部操作日志

看提示,是貌似当前ClassLoader,找不到FastRuntimeEngine

public class GroupTemplate {

    /* 模板在运行过程中,class方法,accessory调用等需要的classLoader */
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader() != null
            ? Thread.currentThread().getContextClassLoader()
            : GroupTemplate.class.getClassLoader();

你可以在构造GroupTemplate前,先设置Thread.currentThread().setContextClassLoader(xxxx),这里的xxx是能加载到GroupTemplate的

正常情况springboot 不应该这样,你有什么特殊处理么,或者把你的这个工程放到git上,我下载运行看看

输入图片说明

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping("/")
    public Object test() {
        String msg = "当前时间为:" + LocalDateTime.now();
        Map map = new HashMap<>();
        map.put("code:", "ok");
        map.put("msg", msg);
        CompletableFuture.supplyAsync(() -> {
            Map map3 = new HashMap();
            map3.put("name", "上海");
            map3.put("name1", "北京");
            map3.put("name2", "广州");
            List<String> templates = Arrays.asList("hello,${name}", "world,${name1}", "test,${name2}");
            return templates.stream().map(s -> BeetlTemplateUtils.render(s, map3)).collect(Collectors.toList());
        }).whenComplete((t, e) -> {
            if (e != null) {
                e.printStackTrace();
            } else {
                System.out.println(t);
            }
        });
        return map;
    }
}

JDK11,你可以简单打个包出来验证一下

@乀柿子不軟丷 BeetlTemplateUtils 咋写的呢

每次都会在里面创建一个 GroupTemplate么?

@Slf4j
public class BeetlTemplateUtils {

    protected static final GroupTemplate STRING_GROUP_TEMPLATE;

    protected static final GroupTemplate CLASS_PATH__GROUP_TEMPLATE;


    static {
        Configuration cfg = null;
        try {
            cfg = Configuration.defaultConfiguration();
        } catch (IOException e) {
            log.error("模板引擎初始化错误:", e);
        }
        //string template
        STRING_GROUP_TEMPLATE = new GroupTemplate(new StringTemplateResourceLoader(), cfg);
        STRING_GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
        //class path template
        CLASS_PATH__GROUP_TEMPLATE = new GroupTemplate(new ClasspathResourceLoader("template/"), cfg);
        CLASS_PATH__GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
    }


    /**
     * 渲染模板
     *
     * @param templateContent
     * @param values
     * @return
     */
    public static String render(String templateContent, Map<String, Object> values) {
        Template template = STRING_GROUP_TEMPLATE.getTemplate(templateContent);
        values.forEach((k, v) -> template.binding(k, v));
        return template.render();
    }

    /**
     * 渲染模板
     *
     * @param fileName
     * @param values
     * @return
     */
    public static String renderClassPath(String fileName, Map<String, Object> values) {
        Template template = CLASS_PATH__GROUP_TEMPLATE.getTemplate(fileName);
        values.forEach((k, v) -> template.binding(k, v));
        return template.render();
    }


}

好的,神奇了

@乀柿子不軟丷 java8+springboot2.4 是正常的,我再试试jdk11吧

@乀柿子不軟丷 在idea下,jdk11和jdk8+springboot2.4,没问题

但是,打包成jar后,jdk8没有问题,jdk11报错了(但页面还是输出了结果),这点就很神奇了

我做了如下修改,可以用,但具体在11下,你的方式为什么不行,我还需要研究

@Configuration
public class TemplateConfig {

	public BeetlTemplateUtils beetlTemplateUtils(){
		BeetlTemplateUtils templateUtils = new BeetlTemplateUtils();
		return templateUtils;
	}
}

@Component
public class BeetlTemplateUtils {
	protected GroupTemplate STRING_GROUP_TEMPLATE;

	protected GroupTemplate CLASS_PATH__GROUP_TEMPLATE;

	public BeetlTemplateUtils() {
		Configuration cfg = null;
		try {
			cfg = Configuration.defaultConfiguration();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("模板引擎初始化错误:");
			throw new RuntimeException(e);
		}
		//string template
		STRING_GROUP_TEMPLATE = new GroupTemplate(new StringTemplateResourceLoader(), cfg);
		STRING_GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
		//class path template
		CLASS_PATH__GROUP_TEMPLATE = new GroupTemplate(new ClasspathResourceLoader("template/"), cfg);
		CLASS_PATH__GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
	}
}
//忽略其他方法

这样,你的controller按照spring方式写就好了,

@Autowired
	BeetlTemplateUtils beetlTemplateUtils;

	
	@GetMapping("/")
	public Object test() {
        }

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
29 xiandafu 1678706040
Java
1
https://gitee.com/xiandafu/beetl.git
git@gitee.com:xiandafu/beetl.git
xiandafu
beetl
Beetl

搜索帮助

53164aa7 5694891 3bd8fe86 5694891