Compiling a Java Class in memory with `lombok` annotations and Java JDK 8

后端 未结 1 1717
醉酒成梦
醉酒成梦 2020-11-30 14:58

I\'m trying to retrieve the description of a few Java Beans from an XML file. I\'d like to annotate them with @Data from project lombok to automati

相关标签:
1条回答
  • 2020-11-30 15:42

    Thanks to Holger, I successfully solved the problem.

    The issue was caused by the absence of tools.jar in the class path. This is due to the fact that Eclipse by default recognises the Java environment as a JRE instead of a JDK.

    On top of that, Java JDK may - or may not, depending on which version you have - have the tools.jar file.

    If you have Java 7 or 8, you should have such library in $JAVA_HOME/lib/tools.jar.

    If you have Java 6, the file is not present but the same functionality is provided by $JAVA_HOME/Classes/classes.jar.

    The compiler is a feature added with Java 6, so if you want to use it and you have an older version of Java, you should update your environment first.

    Now, there are several ways to include tools.jar (or classes.jar) into your project's class path; since I use gradle, I decided to introduce it as a dependency, as you can see in the following snippet of code:

    dependencies {
        compile files("${System.properties['java.home']}/../lib/tools.jar")
        compile 'org.projectlombok:lombok:1.14.4'
        testCompile 'junit:junit:4.11'
    }
    

    Hope this little explanation might help other people facing a similar problem!

    Cheers!

    0 讨论(0)
提交回复
热议问题