What is the alternative of com.sun.tools.javadoc.M

2019-07-25 10:16发布

问题:

I am using JDK 11 on Apache netbeans 10.

The main method is depricated since java 9 and marked for a removal where there is no alternative

All of my attempts in command line ended up with

javadoc: error - Cannot find doclet class Exception

When I try:

com.sun.tools.javadoc.Main.execute (new String[]{"-doclet",TestVarElement.class.getName(),"C:\\Users\\Super3\\Documents\\NetBeansProjects\\MyProject\\src\\pk\\TestVarElement.java"});

I get:

javadoc: error - Doclet class pk.TestVarElement does not contain a start method

Start method is depricated and replaced by run method, the previous set up is working for java 8 and older, I want the equivalent for 9,10,11.

I looked at the documentation for DocumentationTool and the relevent materials but I did not find a single working example.

Is there any way we can run a Doclet/DocletEnvironment programmatically or a working example from commandline?

回答1:

Assuming what you're trying to do is run Javadoc programatically, the jdk.javadoc module documentation describes ways to do so:

javadoc

This module provides the equivalent of command-line access to javadoc via the ToolProvider and Tool service provider interfaces (SPIs), and more flexible access via the DocumentationTool SPI.

Instances of the tools can be obtained by calling ToolProvider.findFirst or the service loader with the name "javadoc".

Based on your previous code, and your edit, you don't need the functionality of the DocumentationTool SPI. The ToolProvider SPI should suffice. As mentioned in the documentation (and by @AlanBateman in the comments) you can get the ToolProvider instance using the following:

ToolProvider javadoc = ToolProvider.findFirst("javadoc").orElseThrow();

You then call one of the run methods with the out/err streams of your choice and the desired arguments. The arguments are the same you'd use for the unsupported Main.execute API you're currently using.

int result = javadoc.run(System.out, System.err, /* ... your args ... */);