4

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.

Community
  • 1
  • 1
Andrei Zafiu
  • 501
  • 6
  • 17
  • Its an old question, but for reference: I use this.getClass().getClassLoader().getResource(".").getFile() to get the directory where the class files are put and at least on netbeans this is under the project root and also resource files get copied over to this location (even on change) – Thomas Oster Aug 08 '19 at 04:50

1 Answers1

0

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.

Community
  • 1
  • 1
kapex
  • 28,903
  • 6
  • 107
  • 121
  • Thank you for your answer. Indeed, in the meantime I switched from JRE 1.7 to jdk 1.7 and added the tools.jar from the lib folder to the jdk. Unfortunately I did not get a null JavaCompiler, just a null `StandardJavaFileManager.getLocation(StandardLocation.SOURCE_PATH)` which means I cannot get the source path. – Andrei Zafiu Aug 09 '14 at 05:59
  • Not working either for me using maven-processor-plugin:2.2.4. fm.getLocation(StandardLocation.SOURCE_PATH) returns null. Only StandardLocations returning not null are useless: CLASS_PATH contains location /opt/buildmanagement/hudson/slaves/slave_1/maven3-agent.jar and /opt/buildmanagement/hudson/tools/apache-maven-3.0.5/boot/plexus-classworlds-2.4‌​.jar; PLATFORM_CLASS_PATH contains some JARs. The rest is null. – JRA_TLL Nov 11 '15 at 11:50