注释处理器无法使用Compiler API-Java

时间:2018-09-24 12:25:01

标签: java lombok java-compiler-api

我有一个任务可以从源代码编译Java源文件。因此,我使用了JavaCompiler API。我的Java类使用Lombok注释@Data进行了注释。但是当我编译类时会抛出异常。

Name.java

package com.pojo;

import java.lang.String;
import lombok.Data;

@Data
public class Name {
  private String last;

  private String first;
}

这是我编译Name.java的代码

String fileToCompile = "/com/pojo/Name.java";

        File file = new File(fileToCompile);

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("/home/")));

        Iterable<? extends JavaFileObject> jf = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(file));

        List<String> options = new ArrayList<>();
        options.add("-processor");
        options.add("lombok.launch.AnnotationProcessorHider$AnnotationProcessor");


        // Compile the file
        compiler.getTask(null,
                fileManager,
                null,
                options,
                null,
                jf)
        .call();

        fileManager.close();

例外:

  

警告:由于(最可能的)原因,无法初始化javac处理器   类加载器问题:java.lang.NoClassDefFoundError:   com / sun / tools / javac / processing / JavacProcessingEnvironment公共类   名称 {          ^在lombok.javac.apt.LombokProcessor.getJavacProcessingEnvironment(LombokProcessor.java:423)     在lombok.javac.apt.LombokProcessor.init(LombokProcessor.java:90)
    在   lombok.core.AnnotationProcessor $ JavacDescriptor.want(AnnotationProcessor.java:124)     在lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:177)     在   lombok.launch.AnnotationProcessorHider $ AnnotationProcessor.init(AnnotationProcessor.java:73)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment $ ProcessorState。(JavacProcessingEnvironment.java:500)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment $ DiscoveredProcessors $ ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment.access $ 1800(JavacProcessingEnvironment.java:91)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment $ Round.run(JavacProcessingEnvironment.java:1035)     在   com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)     在   com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)     在   com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    在com.sun.tools.javac.main.Main.compile(Main.java:523)处   com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
    在com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)     在com.pojo.CompilerTest.main(CompilerTest.java:55)上,由以下原因引起:   java.lang.ClassNotFoundException:   com.sun.tools.javac.processing.JavacProcessingEnvironment位于   java.lang.ClassLoader.findClass(ClassLoader.java:530)在   java.lang.ClassLoader.loadClass(ClassLoader.java:424)在   lombok.launch.ShadowClassLoader.loadClass(ShadowClassLoader.java:530)     在java.lang.ClassLoader.loadClass(ClassLoader.java:357)... 17   更多1条警告

0 个答案:

没有答案