How can I generate javadocs using Java?

故事扮演 提交于 2019-12-08 18:41:42

问题


I'm writing a Java app that will let me compile a Java project based solely on it's file structure so that I can write code and compile without an IDE. The problem that I'm currently having is that I would like to automatically generate the javadoc while I'm compiling, though while Java 6 supplies a JavaCompiler object to work with, I can't find a way to use the javadoc command.

How can I generate the javadoc html for my projects using Java code?


回答1:


Just in case you weren't aware both Apache Ant and Apache Maven are tools that exist to accomplish a similar goal to what you are writing (compiling without an IDE).

Both of them have built in support for generating javadoc. Ant syntax looks like this:

<!-- publish javadoc -->
<target name="javadoc" description="Creates javadoc for IMP.">
      <delete dir="${web-javadoc}"/>
      <javadoc sourcepath="${source}"
               defaultexcludes="no"
               destdir="${web-javadoc}"
               author="true"
               version="true"
               use="true"
               windowtitle="IMP: Integrated Mechanisms Program"
               overview="${source}/overview.html"
               classpathref="debug.classpath"
               stylesheetfile="${javadoc-theme}/stylesheet.css"
       />

       <copy file="${javadoc-theme}/javadoc.jpg" tofile="${web-javadoc}/javadoc.jpg"/>
</target>

If you really want to generate it on your own you want to use the Doclet API

import com.sun.javadoc.*;

public class ListClass {
    public static boolean start(RootDoc root) {
        ClassDoc[] classes = root.classes();
        for (int i = 0; i < classes.length; ++i) {
            System.out.println(classes[i]);
        }
        return true;
    }
}



回答2:


The Javadoc tool has a programmatic interface with public methods for invoking the Javadoc tool from another program written in the Java language. These methods are located in class com.sun.tools.javadoc.Main in lib/tools.jar.

From: http://download.oracle.com/javase/6/docs/technotes/guides/javadoc/standard-doclet.html#runningprogrammatically




回答3:


In a terminal, type javadoc - it will give you a nice list of options. Simplest:

javadoc -d your_output_directory -classpath your_classpath -sourcepath your_sourcefile_dir



回答4:


See Running the Standard Doclet Programmatically in the tool docs.




回答5:


There is a javadoc command, it comes with the JDK(man javadoc on unix platforms)

However, there are better ways. How are you compiling code? If you are using an automated build tool such as ant or maven, you can add a javadoc task to automatically generate the javadocs whenever you compile your code

http://ant.apache.org/manual/Tasks/javadoc.html

for more info on ant(other tools have similar functionality, refer to your tools documentation for more info)

That is what I do, it works brilliantly.



来源:https://stackoverflow.com/questions/7004464/how-can-i-generate-javadocs-using-java

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