2

Using Java 8, it is easy to debug a Doclet: How can I debug a Doclet in Eclipse?

But how to achieve this in Java 10 and subsequent versions? Calling

jdk.javadoc.internal.tool.Main.execute(javadocArgs);

throws an Exception:

Exception in thread "main" java.lang.IllegalAccessError: class my.BDoclet (in unnamed module @0x1f36e637) cannot access class jdk.javadoc.internal.tool.Main (in module jdk.javadoc) because module jdk.javadoc does not export jdk.javadoc.internal.tool to unnamed module @0x1f36e637
Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
wimix
  • 574
  • 1
  • 5
  • 16
  • 2
    The [accepted answer](https://stackoverflow.com/a/7607968/6395627) in the linked question says to use `com.sun.tools.javadoc.Main`... why are you using `jdk.javadoc.internal.tool.Main`? Also, the `com.sun.tools.javadoc.Main` class is marked as `Deprecated`. The documentation of the [`jdk.javadoc`](https://docs.oracle.com/javase/10/docs/api/jdk.javadoc-summary.html) module gives you options for running `javadoc` from code: the `ToolProvider`, `Tool`, and `DocumentationTool` APIs. Otherwise, you could always use `--add-opens` in the command line. – Slaw Jul 17 '18 at 14:59

3 Answers3

1

The javadocArgs is an array containing each argument as a single entry:

    String[] docletArgs = new String[]{
        "-doclet", LatexDoclet.class.getName(),
        "-docletpath", "target/classes/", 
        "-sourcepath", "src/test/java/", 
        LatexDoclet.class.getPackageName()
    };
    DocumentationTool docTool = ToolProvider.getSystemDocumentationTool();
    docTool.run(System.in, System.out, System.err, docletArgs);
Elektrowolle
  • 21
  • 1
  • 4
0

Example based on Slaw's comment:

  String[] javadocArgs = new String[] {...}
  ServiceLoader<Tool> toolService = ServiceLoader.load(Tool.class);
  Optional<Tool> javadocOpt = toolService.stream().map(p -> p.get()).filter(t -> t.name().equals("javadoc")).findFirst();
  javadocOpt.orElseThrow().run(System.in, System.out, System.err, javadocArgs);
wimix
  • 574
  • 1
  • 5
  • 16
  • Thanks for answering your own question. This answer however doesn't make clear if / how / why the code solves the problem. Answers should be self contained and not rely on comments. And if this does answer the question (edit it!!!) then you can mark it as the accepted answer after two days or so. Another hint: you can actually link to comments as well, just right click on the time behind the comment and copy the link. – Maarten Bodewes Jul 29 '18 at 02:04
-1

The same, a bit more elegantly:

//import javax.tools.*;
String[] args = new String[]{"-doclet " + MyDoclet.class.getName()};
DocumentationTool docTool = ToolProvider.getSystemDocumentationTool();
docTool.run(System.in, System.out, System.err, args);

However, it's not clear to me what the "args" should be because it doesn't work and produces:

javadoc: error - invalid flag: -doclet ...MyDoclet

dagnelies
  • 5,203
  • 5
  • 38
  • 56