The service configuration file is incorrect, or the constructor object Processor not found

        The service configuration file is incorrect, or the constructor object j avax.annotation.processing.Processor: Provider com.example.MyProcessor not found        

When you encounter this error, you should be doing compilation-time work, such as generating classes, files, etc. However, it is also Baidu, and there is no problem with the writing method, or it succeeds twice occasionally. The online solution says to add autoService or something, but most of them don't explain why this error is reported.

        First of all, when compiling, javac will look for the class information configured in the file resource/META-INF/services/javax.annotation.processing.Processor in all jar packages and projects (modules), remember that it is class information , it This class will be loaded through the classloader. At this time, because the files in the project (module) are in the compilation period and the class files have not been generated, naturally the corresponding class cannot be found. Therefore: the solution is as follows,

Method 1: Remove the javax.annotation.processing.Processor file, and then copy the file to the corresponding directory after the compilation is complete (but you need to copy it after each compilation)

Method 2: Remove the javax.annotation.processing.Processor file, and add this file to the project (module) that needs to be compiled ( recommended, no dependencies )

Method 3: Use Google's autoService annotation ( recommended )

<!-- https://mvnrepository.com/artifact/com.google.auto.service/auto-service -->
<dependency>
    <groupId>com.google.auto.service</groupId>
    <artifactId>auto-service</artifactId>
    <version>1.0-rc7</version>
</dependency>
@SupportedAnnotationTypes("com.example.MyAnn")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@AutoService(Processor.class)
public class MyProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        // do something
        return true;
    }

}

        No matter which method is used above, it is not possible to compile and generate classes in this project (module). It needs to be packaged into a jar package for another project (module) to use, and can only be viewed in another project (module). to the effect .

Guess you like

Origin blog.csdn.net/wyanyi/article/details/125686058