55

How do you debug java annotation processors using intellij?

Preferably using IDEA IntelliJ. I tried setting a breakpoint inside the processor and running but it did not break.

ManuPK
  • 11,623
  • 10
  • 57
  • 76
akula1001
  • 4,576
  • 12
  • 43
  • 56
  • I'm just starting up a main class that does nothing. I've enabled annotation processing as described [here](http://blogs.jetbrains.com/idea/tag/annotation-processing/). – akula1001 Dec 21 '11 at 09:08
  • 1
    @manu1001, this will not work because annotation processor is running just before compiler runs. Then you start JVM and try to debug it. You can debug your application code but not what is happening in the annotation processor. – AlexR Dec 21 '11 at 10:36
  • but the processing code itself must run under jvm, so theoretically shouldn't there be a way to debug it? – akula1001 Dec 21 '11 at 10:43
  • 1
    You are right. It runs under JVM. But it is other instance of JVM. `javac` runs JVM that runs processors, then compiles the code. Then your IDE runs yet another JVM that runs your application. You want to debug the *first* JVM, i.e. one that is executed by compiler. – AlexR Dec 21 '11 at 11:54
  • Just a comment that javac seems to skip annotation processors completely and silently for some parsing errors. For me a duplicated annotation caused by a merge mistake caused thousands of failures because my annotation processors didn't run - so the real error got completely lost. – mjaggard Sep 16 '22 at 09:39

8 Answers8

41

If you really need to debug an annotation processor, it might be better to run the annotation processor from the command line rather than within your IDE with debugging enabled and attach to that using your IDE's debugger.


If running javac directly, you can debug this by specifying the following extra parameters:

javac -J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 ... (usual javac parameters go here)

If running Maven, use mvndebug instead of the standard mvn command - Maven runs the compiler in-process.


If running Ant, add the following to the ANT_OPTS environment variable before running:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000

With all these executions, the javac, Ant or Maven process will wait for you to attach your debugger before it actually starts executing. IntelliJ instructions for this are here. For Eclipse, here.

prunge
  • 22,460
  • 3
  • 73
  • 80
  • 1
    See also how it's done for AndroidAnnotations' processor [here](http://www.pingtimeout.fr/2012/10/debugging-annotation-processor-in-every.html), the article explains how to debug an annotation processor in every IDE. – Bastien Jansen Apr 11 '13 at 08:08
  • 1
    -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 works for IntelliJ – LostSalad Oct 07 '13 at 13:20
  • For an example of how to use IDEA 2020.1 to debug annotation processors running in a JVM running `javac`, check out this sample: https://github.com/nazmulidris/annotation-processing/tree/main/simple-project#debugging-the-annotation-processors – nazmul idris Jun 27 '20 at 22:46
18

This tutorial is written for an Android project. Main module name is "app" as usual. The project contains a submodule called "annotation" which is subdependency of "app". "app" module runs annotation processing with gradle declaration apt project(':annotation') .

SIMPLE VERSION (run compilation from terminal and attach from IDE)

  1. [REQUIRED] Add a new project configuration "+" -> "Remote". Check "Single instance only". All other settings are generated automatically. Leave <whole project> as the classpath. Port should be left as the default 5005.

enter image description here

  1. [REQUIRED] Make sure you stop all gradle instances by calling: ./gradlew --stop
  2. [REQUIRED] Run the command : ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac

enter image description here

  1. Run the APT project configuration in debug mode as fast as possible :)

enter image description here


  1. [HINT] We start with an EMPTY gradle.properties file
  2. [HINT] DO NOT USE gradle daemon ( --no-daemon / org.gradle.daemon=false option )
  3. [HINT] Run gradle in debug mode ( org.gradle.debug=true option )
  4. [HINT] Run app's module compilation not the processor's module compilation (app's compilation runs annotation processing!)
  5. We DO NOT normally add any Java compiler settings in Android Studio (i.e. File -> other settings -> Default settings)

EXTENDED VERSION (use gradle.properties)

  1. Add the following to your gradle.properties file:

    org.gradle.daemon=false
    org.gradle.debug=true

  1. Run the compilation from terminal:

./gradlew :app:clean :app:compileDebugJavaWithJavac

enter image description here


ADVANCED VERSION (just press debug in IDE)

  1. Add a bash script to your project main dir (e.g. compile.sh)
#!/bin/bash
./gradlew :app:clean :app:compileDebugJavaWithJavac &

Remember about the '&' sign for background processing.

  1. Go to APT configuration settings we created in step 1 and add a Before launch configuration. Select Run external tool.

enter image description here

  1. Add the path to the compile.sh script we created earlier.

enter image description here


Warning

Messing up gradle compilation, NullPointer exceptions during compilation etc. sometimes result in AndroidStudio being stuck (frozen on gradle refresh stage). If you cannot stop gradle from the IDE then use this command in the terminal:

ps -A | grep gradle | awk '{ print $1; }' | xargs kill -9

Turning off debug option during project refresh sometimes helps Android Studio to come back to the right track.

16

Follow these steps, These worked for me on android studio for gradle project:-

1).In gradle.properties add following lines

org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

2).Edit Build COnfiguration and add Remote Configration enter image description here

3).Run recently created run configuration APT.

4).Add break point in annotation processing code and build overall project

Raniz
  • 10,882
  • 1
  • 32
  • 64
Gagandeep Singh
  • 17,273
  • 1
  • 17
  • 18
  • 2
    After creating the configuration, I get a "Can't find or load main class agentlib:jdwp[...]" error, and I don't have a clue why. – Dabbler Sep 18 '16 at 11:01
  • 2
    A missed step is to run the javac task from cmd `./gradlew.bat app:compileDebugJavaWithJavac --debug` then immediately start the created configuration task from IDE. – M. Reza Nasirloo Feb 01 '17 at 13:44
8

It is possible to run javac and debug it, as indicated higher. However in my case it was tedious to write the complete classpath, so I wanted to leave this to IDEA. So in the module where I wanted to apply my annotation processor, just create a class with main method.

public static void main(String[] args) {
    com.sun.tools.javac.Main.main("-proc:only",
        "-processor", "my.pkgs.MyAnnotationProcessor",
        "my/pkgs/any/ClassIWantProcess.java");
}

For this to work you need to add $JAVA_HOME/lib/tools.jar to your SDK's JARs (by default it is not there). This is the same reason why appservers compiling JSPs need this JAR on their classpath - they need the compiler.

Then just set proper working directory for your run configuration (so the relative path to the java file is correct), set your break-point into the processor and debug at your will!

Benefit - classpath is set already by IDEA and used by the "inner" javac, because here it is not a separate process. I believe it's possible to translate it to other IDEs too.

virgo47
  • 2,283
  • 24
  • 30
  • I just went through this process and I prefer this solution because it uses the -processor flag which throws more detailed errors than the --processorpath or -cp javac debugging routes. My issue turned out to be that the class files I was producing weren't compatible with where I was running them. – PaulR Dec 15 '15 at 01:11
  • While I know these comments don't have much value, but this is indeed what I ended up doing (added the `JDK\libs\tools.jar` to the project as dependency and executed javac for the file `"c:/Development/HomeProjects/funky-apt/src/main/java/com/zhuinden/App.java"` (absolute path)) and I can indeed debug my annotation processor. Great answer. – EpicPandaForce Apr 10 '17 at 12:39
5

For a Maven project, this post, which explains the following steps in a little more detail, worked for me:

  1. Add "Remote" run configuration and set "port" to 8000.

  2. Issue the command mvnDebug clean install from the project's directory (on the command line).

  3. Run the run configuration. In order to start a new session after the processes quit, repeat from (2).

Remember to run mvn install on the project's dependencies when they change (e.g. if the annotation processor is in a different artifact than the project you are debugging it from).

bisgardo
  • 4,130
  • 4
  • 29
  • 38
3

Annotation processing occurs during compilation, so normal debugging won't work. If you want to debug it in the context of you project, you can use IntelliJ remote debugging, while having Gradle or Maven in debug mode. Then you can put breakpoints in the Annotation Processor's files.

See Debugging an Annotation Processor in any project.

Disclaimer: I wrote the post.

bryant1410
  • 5,540
  • 4
  • 39
  • 40
3

Debugging an annotation processor with IntelliJ IDEA and Gradle

  1. Set a custom VM option -Dcompiler.process.debug.port=5005: press Ctrl + Shift + A and select Edit Custom VM Options... in the list of actions to add a custom VM option then restart the IDE.
  2. Create a remote debug configuration with default parameters: Run -> Edit Configurations... -> Add New Configuration (Alt + Insert) -> Remote. enter image description here
  3. Set breakpoints.
  4. Build with Gradle from the terminal: $ ./gradlew --no-daemon -Dorg.gradle.debug=true clean build (it's okay if the execution of the command is frozen, don't terminate a process).
  5. Debug the remote debug configuration within the IDE (see step 3): select a suitable remote debug configuration and press Shift + F9. enter image description here

Hope it helps somebody :)

Yurii Rabeshko
  • 591
  • 8
  • 17
3

I found the following resource that can help you: http://code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse

The guy explains step-by-step how to debug annotation processors using Eclipse.

AlexR
  • 114,158
  • 16
  • 130
  • 208