有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误的服务配置文件,或在构造处理器对象时引发异常

我正在用Java编写一个简单的自定义注释,但遇到了一个问题。下面是我代码的主要部分

日志自定义注释。java

package fun.n.learn.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// We need this annotation only till before compilation.
@Retention(RetentionPolicy.SOURCE)
// This is a simple custom annotation.
public @interface LogMeCustomAnnotation {

}

LogMeCustomAnnotationProcessor。java

package fun.n.learn.annotation;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

// List the custom annotations that are supported.
@SupportedAnnotationTypes({ "fun.n.learn.annotation.LogMeCustomAnnotation" })
// Extend AbstractProcessor. This will let you process.
public class LogMeCustomAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {

        Messager messager = processingEnv.getMessager();
        messager.printMessage(Diagnostic.Kind.NOTE, "I was here.");

        // TODO: Put some meaningful code here. Right now just get it to work.

        // return false;
        // We have already handled these annotations. No more. So return true.
        return true;
    }

}

/src/main/resources/META-INF/services/javax。注释。处理。处理器

fun.n.learn.annotation.LogMeCustomAnnotationProcessor

pom。xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>fun.n.learn</groupId>
    <artifactId>javaCustomAnnotation</artifactId>
    <version>0.1.0</version>

    <build>
        <plugins>
            <plugin>
                <!-- Configure the project to use java 8 version. -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- Disable annotation processing for ourselves. -->
                    <!-- <compilerArgument>-proc:none</compilerArgument> -->
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

现在,当我运行mvn -e clean install时,我遇到了以下问题

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider fun.n.learn.annotation.LogMeCustomAnnotationProcessor not found
[INFO] 1 error

我一定错过了一个简单的技巧。有什么帮助吗


共 (6) 个答案

  1. # 1 楼答案

    通过删除annotation processor的目标文件夹并重新编译框架,我已经解决了以下问题。它起了神奇的作用

    问题:

    java: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider com.tracelink.automation.framework.processors.FeedFactoryProcessor not found

  2. # 2 楼答案

    尝试使用以下标志编译它:-proc:none

  3. # 3 楼答案

    我特别遇到了这个错误

    Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider com.iviz.schemarestriction.processor.SchemaRestrictionCompilationProcessor could not be instantiated
    

    将maven项目从JDK1.8(1.8.0201)迁移到OpenJDK11(11.0.2)时

    通过添加对的依赖项(2.3.1是最新的稳定版本)对其进行了修复

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    
  4. # 4 楼答案

    嗯。发现了问题。早些时候我的pom。xml注释掉了proc:none行。现在我已经把它重新投入使用,一切都很好。我需要弄清楚这一行到底是做什么的,但我的问题的答案是把proc:none放回游戏中。这就是我的pom的构建部分的工作方式。xml现在看起来不错

    <build>
        <plugins>
            <plugin>
                <!-- Configure the project to use java 8 version. -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- Disable annotation processing for ourselves. -->
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  5. # 5 楼答案

    默认的maven生命周期使用javax运行javac。注释。处理。处理器文件作为类路径的一部分。这会导致编译器期望文件中列出的注释处理器的编译实例。但LogMeCustomAnnotationProcessor此时未编译,所以编译器会引发“错误的服务配置文件…”错误见bug report

    为了解决这个问题,maven的编译阶段可以分开,首先编译注释处理器,然后编译整个项目

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <configuration>
                            <compilerArgument>-proc:none</compilerArgument>
                            <includes>
                                <include>fun/n/learn/annotation/LogMeCustomAnnotationProcessor.java</include>
                                <!--include dependencies required for LogMeCustomAnnotationProcessor -->
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile-project</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    default-compile执行在禁用注释处理的情况下编译LogMeCustomAnnotationProcessor,以便成功编译。
    compile-project使用注释处理编译整个项目

  6. # 6 楼答案

    请按照以下步骤解决此问题:

    • 编辑nbproject/project.properties文件
    • 搜索javac.processorpath,并将其更改为:

    javac.processorpath=\ ${javac.classpath}:\ ${libs.eclipselink.classpath}