How can I get the working directory of a project from an Annotation Processor in Java

倖福魔咒の 提交于 2020-01-04 04:20:26

问题


I am writing an annotation processor in Java and in this annotation processor I want to be able to find a file in the Project hierarchy of the project on which I am using this annotation processor. Through the annotation I can pass in the path of the file I am searching for relative to the project root but i cannot retrieve the project's working directory.

Let's say that the processor is MyCustomProcessor and I am using it on the project MyProject. I want to be able to access(read) a file (a properties file) from the project structure of MyProject from the "process" method of MyCustomProcessor.

I have read this link Eclipse - Annotation processor, get project path but when I use their solution I get a null returned from the StandardJavaFileManager.getLocation(StandardLocation.SOURCE_PATH) call.

Some more details regarding the implementation:

MyAnnotationProcessor:

@SupportedAnnotationTypes(value = {"MyAnnotation" })
@SupportedSourceVersion(RELEASE_6)
public class MyCustomProcessor extends AbstractProcessor {
...

@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
    for (final Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
<!-- Here is where I would like to get the working directory !-->
}
 }
}

More details about the testing and development environment: Eclipse Kepler, JRE 1.7.

If you need more details just ask.


回答1:


A comment on that answer points to this question as a solution: null JavaCompiler in Eclipse

Some tools aren't implemented in the JRE and only available in the JDK. It seems Eclipse runs the processor using the JRE by default, so you need to configure your project to use a JDK as runtime instead.



来源:https://stackoverflow.com/questions/25192381/how-can-i-get-the-working-directory-of-a-project-from-an-annotation-processor-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!