无法运行Oracle提供的Doclet类

时间:2019-03-25 11:22:23

标签: java javadoc java-9 doclet

我想尝试Doclets( JDK 9 ),所以我逐步尝试了此link.中给出的所有内容,但我没有在Example类中添加任何自定义内容。正是Oracle所提供的(我刚刚添加了导入)。

Oracle提供命令:

javadoc -doclet Example \
       -overviewfile overview.html \
       -sourcepath source-location \
       source-location/Example.java

现在,当我运行给定的 javadoc 命令时,出现以下错误:

  

javadoc:错误-找不到doclet类示例

我尝试了命令的一些变体,因为这似乎是导演的问题,但是我所有的尝试都失败了。

我将Example.java放在我的桌面文件夹中:C:\Users\George\Desktop\

因此,在我的命令行中,我cd C:\Users\George\Desktop\。然后javac Example.java(以防它需要编译)。

然后,我尝试以下所有命令,并得到相同的错误。

javadoc -doclet Example -overviewfile overview.html -sourcepath ./ ./Example.java

javadoc -doclet Example -overviewfile overview.html -sourcepath "C:\Users\George\Desktop\" "C:\Users\George\Desktop\Example.java"

(不带引号)

javadoc -doclet Example -overviewfile overview.html"C:\Users\George\Desktop\Example.java"

我尝试了SO中发现的其他几件事,但同样没有任何效果。 我想念什么?给定的示例不应该工作吗?

示例类(如果您看到我看不到的东西)

public class Example implements Doclet {
    Reporter reporter;
    String overviewFile;

    public static void main(String[] args) {
    }

    public Example() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init(Locale locale, Reporter reporter) {
        reporter.print(Kind.NOTE, "Doclet using locale: " + locale);
        this.reporter = reporter;
    }

    public void printElement(DocTrees trees, Element e) {
        DocCommentTree docCommentTree = trees.getDocCommentTree(e);
        if (docCommentTree != null) {
            System.out.println("Element (" + e.getKind() + ": " + e + ") has the following comments:");
            System.out.println("Entire body: " + docCommentTree.getFullBody());
            System.out.println("Block tags: " + docCommentTree.getBlockTags());
        }
    }

    @Override
    public String getName() {
        return "Example";
    }

    @Override
    public Set<? extends Option> getSupportedOptions() {
        Option[] options = { new Option() {
            private final List<String> someOption = Arrays.asList("-overviewfile", "--overview-file", "-o");

            @Override
            public int getArgumentCount() {
                return 1;
            }

            @Override
            public String getDescription() {
                return "an option with aliases";
            }

            @Override
            public Option.Kind getKind() {
                return Option.Kind.STANDARD;
            }

            @Override
            public List<String> getNames() {
                return someOption;
            }

            @Override
            public String getParameters() {
                return "file";
            }

            @Override
            public boolean process(String opt, List<String> arguments) {
                overviewFile = arguments.get(0);
                return true;
            }
        } };
        return new HashSet<>(Arrays.asList(options));
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latest();
    }

    @Override
    public boolean run(DocletEnvironment docEnv) {
        reporter.print(Kind.NOTE, "overviewfile: " + overviewFile);
        // get the DocTrees utility class to access document comments
        DocTrees docTrees = docEnv.getDocTrees();

        // location of an element in the same directory as overview.html
        try {
            Element e = ElementFilter.typesIn(docEnv.getSpecifiedElements()).iterator().next();
            DocCommentTree docCommentTree = docTrees.getDocCommentTree(e, overviewFile);
            if (docCommentTree != null) {
                System.out.println("Overview html: " + docCommentTree.getFullBody());
            }
        } catch (IOException missing) {
            reporter.print(Kind.ERROR, "No overview.html found.");
        }

        for (TypeElement t : ElementFilter.typesIn(docEnv.getIncludedElements())) {
            System.out.println(t.getKind() + ":" + t);
            for (javax.lang.model.element.Element e : t.getEnclosedElements()) {
                printElement(docTrees, e);
            }
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

运行javadoc --help会显示以下选项:

-docletpath <path>
              Specify where to find doclet class files

使用该选项,它应该可以正常工作(前提是您编译了Example类,并且确实位于作为此选项的参数传递的目录中)。