19

I am creating a custom doclet that I want to run in my Maven build with the Javadoc plugin, but right now I'd like to test / debug the Doclet in Eclipse. How can I do that?

Do I have to invoke javadoc programmatically? And how?

Sean Patrick Floyd
  • 274,607
  • 58
  • 445
  • 566
  • Sean, good question: I am facing the same challenge of debugging doclets; although the message from the Eclipse dev group points to a pretty ugly method, it took me I think 30 minutes but it works. If anyone finds an easier way, please post :-). Anyway, +1 Sean for the question and for the link. – acostache May 31 '11 at 07:38
  • the link in the question is broken – Omnipresent Jul 07 '11 at 22:02
  • true, deleted it. It was terrible anyway – Sean Patrick Floyd Jul 08 '11 at 04:55

3 Answers3

16

you can simply create a main method in your doclet and call (example, see full cmdling reference):

public class MyDoclet extends Doclet {

    public static void main(String[] args) {
        com.sun.tools.javadoc.Main.execute("-doclet " + MyDoclet.class.getName());
    }
}

That also works with the debugger.

You might also have to add the -classpath parameter containing all jar dependencies needed to parse the actual code.

Jan
  • 2,665
  • 6
  • 33
  • 52
  • 1
    You need to call `com.sun.tools.javadoc.Main.execute(new String[] { "-doclet " + MyDoclet.class.getName() });` at least since Java 8, otherwise compiler fails with `The method execute(String[]) is ambiguous for the type Main` – Marteng Nov 07 '19 at 08:11
2

If you are running JDK v1.8, you may need to use the following code snippet:

Main.execute(docletFqcn.getClass().getClassLoader(), "-doclet", docletFqcn, javaSourceFilePath);

where docletFqcn is the fully qualified class name of your Doclet class and javaSourceFilePath the location of the Java file to process.

Édouard Mercier
  • 445
  • 5
  • 11
0

I got error message with @Jan answer

Error:(13, 35) java: reference to execute is ambiguous
  both method execute(java.lang.String...) in com.sun.tools.javadoc.Main and method execute(java.lang.String,java.lang.String...) in com.sun.tools.javadoc.Main match

After change to these code and it work well

com.sun.tools.javadoc.Main.execute(new String[]{
                "-doclet", CustomDoclet.class.getName(),
                "path/to/src/XXX.java"
        });
user1686407
  • 695
  • 9
  • 15