What is the javadoc utility and what can it do for you?

Documenting a program, and keeping that documentation up to date, is very important, no matter what programming language you happen to be using. Virtually every programming language provides a way for programmers to comment their code so that they and others can better understand what the code is trying to do. Most software projects have additional "external" documentation as well, some for the developers themselves, and some for the end users of the "released" version of the software. Preparing such documentation and keeping it up to date has always been one of the developer's worst nightmares.

The javadoc utility that comes with the JDK creates separate documentation (in the form of linked HTML pages) from comments placed in Java source code, provided those comments follow certain conventions. A major advantage of this is that so long as the comments in the source code itself are kept current (and why shouldn't they be?), the external documentation can be brought up to date at any time simply by re-running the javadoc utility.

If you are even passably familiar with the documentation provided for the JDK itself, then you will be pleasantly surprised to learn that the documentation produced by javadoc for your own code will have the same appearance and structure as this standard documentation. Thus, knowledgeable users who wish to understand your code will already be familiar with your documentation style.

But don't forget ...

The documentation produced by javadoc is aimed, for the most part, at clients of your code, not at those who might have to maintain or extend it (though it might be useful for them as well). With some additional work it can also be directed at end-users of a complete program. In any case, these documentation comments should not be viewed as a replacement for the usual // and /* ... */ kinds of comments that you should be providing for readers of your source, as always. These two kinds of comments are ignored by javadoc.

When should javadoc comments be prepared?

This is a matter of personal preference, unless you are working in a team environment and the team dictates otherwise. You may find it useful to prepare such comments up front as a form of guidance for yourself. The code does not have to be present for javadoc to do its thing. If you do this, it will be important to modify the comments as required as the development proceeds.

Or, you may write your code and insert only the usual // and /* ... */ kinds of comments as you go, waiting until later, or even till the end, to add the "doc comments".

How do you write javadoc comments? (See also here.)

The special kinds of comments that javadoc recognizes are called, not surprisingly, javadoc comments but you will also see them referred to more generically as documentation comments, or simply as doc comments. Each such comment begins with /** and ends with */, and may contain one or more special tags of the form @tagname, each with its associated information.

As is often the case, the best way to get familiar with javadoc and what it can do is to look at a few typical examples, and those are available elsewhere. On this page, however, we simply provide a brief summary of the main things you need to know, and a number of useful tables.

Typically, the information you will want to provide via doc comments falls into two broad categories:

A typical javadoc comment always appears immediately above the entity it is documenting, and has this general form:

/**
 * A sentence or short paragraph of general description for
 * the class, interface, method or field being documented.
 * The first sentence should be given special attention,
 * since it will be extracted to serve as an even shorter
 * summary of the item.
 *
 * @tag1 description
 * @tag2 description
 */

As illustrated above, each documentation comment must be placed within /** ... */ delimiters. The other leading asterisk (*) characters that appear at the left of each line are optional (starting with javadoc version 1.4), and in any case are removed from the final documentation by javadoc. Nevertheless, they are generally included by convention. Each tag is a special keyword that starts with the @ character and serves as an indicator of some special kind of descriptive information. (See the tables below.)

Alphabetical List of Tags
Tag Name and Syntax Purpose and Description
@author author_info Supplies the name and possibly other info for the author of a class or interface. Use a separate tag for each author or piece of information. Not processed unless -author command-line option used when javadoc is run.
@deprecated Used to designate a feature that remains in the code for backward compatibility but that has been superseded by improved features. You should not use any feature so marked in new code, since it might be removed in future versions. Anything marked as deprecated will cause the compiler to issue a warning.
@exception Same as @throws (see below). It is recommended to use @throws rather than @exception.
{@link entity description } See @see below, but note that the braces are required for the @link tag.
@param name description Supplies the name and description of a method parameter. Use a separate tag for each parameter.
@return description Describes the return value when the return type is something other than void.
@see anchor
@see entity description
Causes a separate "See Also" reference link to the given item to be inserted. Similar to @link, except a @link reference is inserted in-line. In the first form anchor is a link to an absolute or relative URL. In the second form an entity specifies the name of the item and description is the text displayed for that item. An "entity" may be any of
packageName.ClassName
packageName.ClassName#methodName(Type1, Type2, ...)
packageName.ClassName#fieldName
in which packageName, ClassName, methodName, or fieldName may appear by themselves. If present, description is used as the text of the link; otherwise, the entity name itself is used.
@serial description Documents a field that is serializable by default.
@serialData description Documents data that is not serializable by default and that is written by methods writeObject() or writeExternal() and read by readObject() or readExternal().
@serialField name type description Documents an object of type ObjectStreamField.
@since date_and/or_version_info Used to indicate when a feature was introduced into the code.
@throws ClassName description Supplies the class name and description of an exception thrown by a method. Use one tag for each exception. Same as @exception, but prefer this tag to @exception.
@version version_info Supplies the version information, if required. Not processed unless -version command-line option used when javadoc is run.

 

Where can each of these tags be used?
For documenting the overview @author, @since, @version
For documenting packages @author, @serial, @since, @version
For documenting classes and interfaces @author, @deprecated, @since, @version
For documenting methods @deprecated, @exception, @param, @return,
@serialData, @since, @throws
For documenting fields @deprecated, @serial, @serialField, @since
For inserting links into your documentation @see and {@link} may be used anywhere

Sun recommends that tags be included in the following order:

@author       (classes and interfaces only)
@version      (classes and interfaces only)
@param        (methods and constructors only)
@return       (methods only)
@exception    (@throws is a synonym added in Javadoc 1.2)
@see
@since
@serial       (or @serialField or @serialData)
@deprecated

Here are some style guidelines from Sun for writing javadoc comments:

Making up your own javadoc tags

The obvious first question here is this: Why would you want to do such a thing? Well, for example, javadoc does not (unlike the doxygen tool, for example) have @pre and @post tags for the pre-conditions post-conditions of a method. So you might well want to "invent" these tags for use in your javadoc comments. The nice thing is that you can do so.

The first thing you need to know, however, is that if you try to use @pre and @post directly, javadoc will tell you that these might be implemented as standard tags sometime in the future and to make sure your code doesn't conflict with some later standard you should use a period in any name that you pick. So ... let's pick @pre.c and @post.c for our tags. The we can use them just like any other tags in the javadoc comments of our methods. But we have to tell javadoc that we are doing this, which we do with the command

javadoc -d html -tag pre.c:mc:"Pre-conditions:" -tag post.c:mc:"Post-conditions:" MyClass.java
which will place HTML documentation for MyClass in the html subdirectory and process any @pre.c and @post.c tags in the process. The three-part value for the -tag option contains

What "source files" does javadoc process?

The javadoc utility will generate output which comes from four different types of "source" files:

  1. Class and interface source code files.
  2. Package comment files (from a file called package.html in each package directory).
  3. Overview comment file. This is an HTML file called whatever you like (but typically overview.html) and containing in its body whatever "overview" documentation you want for a set of packages or a complete application.
  4. Miscellaneous unprocessed files, which might include images, sample source code files, applets, and HTML files which are simply too voluminous for inclusion as doc comments in a normal Java source file. These files should be placed in a directory called doc-files, which can be a subdirectory of any package directory containing source files.

How do you run javadoc?

The javadoc program has many options, which may be viewed by entering the command

javadoc -help

and many of which are listed in the table at the end of this section.

Here is a typical use of the utility. Suppose your current directory is your "project directory" in which you have a subdirectory, say src, containing all your *.java source code files that, in turn, contain all your doc comments that you want to turn into viewable documentation in the form of linked HTML files. Suppose this "project directory" also contains an empty subdirectory called docs where you want to store those HTML documentation files. Then you may accomplish your goal by giving this command:

javadoc -d docs src/*java

If src also contains a package called mypackage in a subdirectory called mypackage and you wish to include this package and its doc comments, then use this command:

javadoc -d docs src/*java src/package/*java

Finally (in this very short summary), if you want an "overview" page (i.e., a page that presents an overview discussion of your application, you need to prepare it in a separate ov_file.html and supply its name as a command-line option like this:

javadoc -overview ov_file.html -d docs src/*java src/package/*java

This overview file should be just an "ordinary" HTML file, from which you want the contents between the <body> ... </body> tags to be made available.

Command-Line Options
Option Name and Syntax Purpose and Description
-author Includes in the documentation the author information from any @author tags in the input source files. This information is omitted by default.
-version Includes in the documentation the version information from any @version tags in the input source files. This information is omitted by default.
-d directory Places the documentation output files in directory, a very useful option which keeps your current directory from being cluttered up with javadoc output files.
-classpath pathList Causes classes to be sought on the specified paths, which overrides the CLASSPATH environment variable. If neither is specified, the current directory is used. Each "path" in pathList is either a directory, a jar file, or a zip file. The paths in pathList must be separated by a platform-dependent separator (; on Windows and / on Unix/Linux, for example).
-sourcepath pathList Causes source files to be sought on the specified paths. If not specified, source files are sought in the class path.
-link URL Creates a link to another set of javadoc files. It is recommended practice to include a link to the standard libary documentation, either locally, or on the Sun web site at http://java.sun.com/products/jdk/1.4/docs/api.
-overview HTMLfile Causes the content of the body of HTML files to be merged with the overview

What files are produced by javadoc?

The number of files produced by javadoc can be intimidating at first, since even a typical pedagogical example can produce a dozen or more distinct files. For this reason, it is a good idea to place them in their own separate subdirectory, often (as illustrated in the previous section) called docs by convention, but you can call it whatever you like.

The table below shows some of the files that show up when you run javadoc. You don't always want all of these files, and you can suppress many of these files by using the appropriate command-line options when javadoc is run. (See the table at the end of the previous section.)

Name of File Description
Basic Content Pages
ClassName.html There will be one of these for each documented class.
InterfaceName.html There will be one of these for each documented interface.
package-summary.html There will be one of these for each package documented. A package should be documented by placing a file called (literally, for any package) package.html in the subdirectory containing the package. This file should contain the desired documentation in its body (between the <body> tags).
Cross-Reference Pages
index-all.html An index file of all class, interface, constructor, method and field names, arranged alphabetically. Can be generated as a single file, or as a separate file for each starting character.
overview-summary.html An overview page for the entire set of packages or application. This is the front page of the generated document. The Javadoc tool will include any HTML text provided in a file specified with the -overview option. Note that this file is created only if you pass into javadoc two or more package names.
overview-tree.html A class hierarchy file for the entire set of packages.
allclasses-frame.html Lists all classes for all packages, used in lower-left frame.
allclasses-noframe.html Lists all classes for all packages, when no frames are being used.
constant-values.html Lists the values of static fields (class variables).
deprecated-list.html Lists all deprecated names.
packages.html Appears to be a deprecated file that redirects you to either a frames version of the documentation's "front page", or a no-frames version.
serialized-form.html Contains information about serializable and externalizable classes.
Support Files
index.html Creates HTML frames for display. The file contains no text content itself, but is the file you load to display the "front page" of the documentation with frames.
help-doc.html A help page describing the navigation bar and the other pages.
stylesheet.css A style sheet (CSS format) that controls the colors, fonts and positioning on the pages generated by javadoc.
package-list A textfile (not an HTML file) that is used by the -link and -linkoffline command-line options, and is not reachable by any of the links in the HTML files.