java 错误的服务配置文件,或构造 Processor 对象时抛出的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36248959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bad service configuration file, or exception thrown while constructing Processor object
提问by mishtusorous
I am writing a simple custom annotation in Java and running into a problem with it. Here is the main parts of my code.
我正在用 Java 编写一个简单的自定义注释,但遇到了问题。这是我的代码的主要部分。
LogMeCustomAnnotation.java
LogMeCustomAnnotation.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
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.annotation.processing.Processor
/src/main/resources/META-INF/services/javax.annotation.processing.Processor
fun.n.learn.annotation.LogMeCustomAnnotationProcessor
pom.xml
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>
Now when I run mvn -e clean install
I get the following problem
现在,当我运行时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
I must be missing a simple trick here. Any help?
我一定在这里错过了一个简单的技巧。有什么帮助吗?
回答by Evgeny
The default maven lifecycle runs javac with javax.annotation.processing.Processorfile as a part of classpath. This cause compiler to expect a compiled instance of annotation processors listed in the files. But LogMeCustomAnnotationProcessor
is not compiled at that moment so compiler raises "Bad service configuration file ..." error. See bug report.
To solve this issue maven compilation phase can be separated to compile annotation processor at the first place and then compile whole project.
默认的 maven 生命周期运行 javac 并将javax.annotation.processing.Processor文件作为类路径的一部分。这导致编译器期望文件中列出的注释处理器的编译实例。但LogMeCustomAnnotationProcessor
当时并未编译,因此编译器会引发“错误的服务配置文件...”错误。请参阅错误报告。
为了解决这个问题,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
execution compiles LogMeCustomAnnotationProcessor
with disabled annotation processing in order to have successful compilation.
compile-project
compiles whole project with annotaton processing.
default-compile
执行编译LogMeCustomAnnotationProcessor
时禁用注释处理,以便成功编译。
compile-project
使用注释处理编译整个项目。
回答by Babatunde Adeyemi
Follow the following steps to resolve this:
请按照以下步骤解决此问题:
- Edit
nbproject/project.properties
file - Search for
javac.processorpath
, and change it to:
- 编辑
nbproject/project.properties
文件 - 搜索
javac.processorpath
,并将其更改为:
javac.processorpath=\
${javac.classpath}:\
${libs.eclipselink.classpath}
javac.processorpath=\
${javac.classpath}:\
${libs.eclipselink.classpath}
回答by mishtusorous
Ok. Found the issue. Earlier my pom.xml had the proc:none
line commented out. Now that I have got it back in action it is compiling fine. I need to find out exactly what this line does, but the answer to my question is just put the proc:none
back in game. This is how the build section of my pom.xml looks now.
行。发现问题。早些时候我的 pom.xmlproc: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>
回答by Tomá? Bahník
I've encountered this error, in particular
我遇到过这个错误,特别是
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
when migrating maven project from JDK 1.8 (1.8.0_201) to OpenJDK 11(11.0.2).
将 maven 项目从 JDK 1.8 (1.8.0_201) 迁移到 OpenJDK 11(11.0.2) 时。
It was fixed by adding dependency on (2.3.1 was the latest stable version)
它是通过添加对(2.3.1 是最新的稳定版本)的依赖来修复的
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>