Kyle Sletten
Kyle Sletten

Reputation: 5413

How can I generate javadocs using Java?

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?

Upvotes: 7

Views: 8602

Answers (5)

Steve B.
Steve B.

Reputation: 57333

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

Upvotes: 5

JohnKlehm
JohnKlehm

Reputation: 2398

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;
    }
}

Upvotes: 6

user439407
user439407

Reputation: 1756

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.

Upvotes: 0

Ryan Stewart
Ryan Stewart

Reputation: 128909

See Running the Standard Doclet Programmatically in the tool docs.

Upvotes: 1

miku
miku

Reputation: 188154

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

Upvotes: 5

Related Questions