What is the alternative of com.sun.tools.javadoc.Main.execute to run Doclet in jdk 11?

試著忘記壹切 提交于 2019-12-11 07:53:03

问题


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 ... */);


来源:https://stackoverflow.com/questions/54040274/what-is-the-alternative-of-com-sun-tools-javadoc-main-execute-to-run-doclet-in-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!