使用com.sun.tools.javadoc.Main.execute在jdk 11中运行Doclet的替代方法是什么? [英] What is the alternative of com.sun.tools.javadoc.Main.execute to run Doclet in jdk 11?
问题描述
我在Apache netbeans 10上使用JDK 11.
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
当我尝试时:
com.sun.tools.javadoc.Main.execute (new String[]{"-doclet",TestVarElement.class.getName(),"C:\\Users\\Super3\\Documents\\NetBeansProjects\\MyProject\\src\\pk\\TestVarElement.java"});
我得到:
javadoc: error - Doclet class pk.TestVarElement does not contain a start method
Start
方法已被描述并替换为run
方法,以前的设置适用于Java 8及更早版本,我希望等效于9,10,11.
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.
有什么方法可以以编程方式运行Doclet/DocletEnvironment或从命令行运行示例吗?
Is there any way we can run a Doclet/DocletEnvironment programmatically or a working example from commandline?
推荐答案
Assuming what you're trying to do is run Javadoc programatically, the jdk.javadoc
module documentation describes ways to do so:
javadoc
此模块提供等效于通过工具服务提供商接口(SPI),并可以通过
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.
这些工具的实例可以通过调用服务加载程序,名称为"javadoc"
.
Instances of the tools can be obtained by calling ToolProvider.findFirst or the service loader with the name "javadoc"
.
根据您先前的代码和您的编辑,您不需要DocumentationTool
SPI的功能. ToolProvider
SPI应该足够.如文档中所述(并在注释中使用@AlanBateman),您可以使用以下命令获取ToolProvider
实例:
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();
然后使用您选择的out/err流和所需的参数调用run
方法之一.参数与您正在使用的不受支持的Main.execute
API所使用的参数相同.
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 ... */);
这篇关于使用com.sun.tools.javadoc.Main.execute在jdk 11中运行Doclet的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!