1

I'm trying to generate some repositories at runtime for spring data jpa with OpenHFT to reduce boilerplate code. The problem is, when running with IDEA it goes just fine, however if i run it in external tomcat or simply in command line: "mvn spring-boot:run", it fails to recognize external resources such as @Resository and some written classes.

I've tried hard to locate problem but no use, however i've created minimal demo for this problem: github. I also tried to use different classloaders as parameter for OpenHFT's compiler, such as .class, Repository.class and Thread.currentThread().getContextClassLoader(). But still end up in error.

@EventListener
private void initRepositoryMapping(ApplicationReadyEvent event) {
    unMappedWithRepoEntity.add(Department.class);
    unMappedWithRepoEntity.forEach((clazz) -> {
        String className = "com.example.repository.DepartmentRepository";
        String javaCode =
                "\npackage com.example.repository;" +
                "\nimport com.example.entity.Department;" +
                "\nimport org.springframework.stereotype.Repository;" +
                "\nimport java.util.UUID;" +
                "\nimport com.example.repository.BaseRepository;" +
                "\n@Repository" +
                "\npublic interface DepartmentRepository extends BaseRepository<Department, UUID> {}" ;
        logger.info("generating java class : {} code:{}", className, javaCode);
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            logger.info("using classloader: {}", classLoader);
            CompilerUtils.CACHED_COMPILER.loadFromJava(classLoader, className, javaCode);
            System.out.println(Class.forName("com.example.repository.DepartmentRepository"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    });
}
gtBacteria
  • 21
  • 5
  • This seems to be a classloader issue. I would suggest to open an issue with OpenHFT: https://github.com/OpenHFT/Java-Runtime-Compiler – Simon Martinelli May 09 '19 at 13:38
  • Please checkout the latter commit in [my github](https://github.com/JohnChuang33/Problem). I tried using raw JavaCompiler API but still getting error with maven but ok when compiling with `com.example.support.CompilerUtils#main` – gtBacteria May 21 '19 at 11:09

0 Answers0