javadocSection: User Commands (1)Updated: 10 March 2001 |
javadocSection: User Commands (1)Updated: 10 March 2001 |
You can run the Javadoc tool on entire packages, individual source files, or both. In the first case, you pass in as an argument to javadoc a series of package names. In the second case, you pass in a series of source (.java) file names. See EXAMPLES at the end of this document.
During a run, the Javadoc tool automatically adds cross-reference links to package, class and member names that are being documented as part of that run. Links appear in several places:
* Declarations (return types, argument types, field types)
You can add hyperlinks to existing text for classes not included on the command line (but generated separately) by way of the -link and -linkoffline options.
The Javadoc tool produces one complete document each time it is run; it cannot do incremental builds - that is, it cannot modify or directly incorporate results from previous runs of Javadoc. However, it can link to results from other runs, as just mentioned.
As implemented, the Javadoc tool requires and relies on the java compiler to do its job. The Javadoc tool calls part of javac to compile the declarations, ignoring the member implementation. It builds a rich internal representation of the classes, including the class hierarchy, and "use" relationships, then generates the HTML from that. The Javadoc tool also picks up user-supplied documentation from documentation comments in the source code.
In fact, the Javadoc tool will run on .java source files that are pure stub files with no method bodies. This means you can write documentation comments and run the Javadoc tool in the earliest stages of design while creating the API, before writing the implementation.
Relying on the compiler ensures that the HTML output corresponds exactly with the actual implementation, which may rely on implicit, rather than explicit, source code. For example, the Javadoc tool will document default constructors (section 8.6.7 of Java Language Specification) that are present in the .class files but not in the source code.
In many cases, the Javadoc tool allows you to generate documentation from source files whose code is incomplete or erroneous. This is a benefit that enables you to generate documentation before all debugging and troubleshooting is done. For example, according to the Java Language Specification, a class that contains an abstract method should itself be declared abstract. The Javadoc tool does not check for this, and would proceed without a warning, whereas the javac compiler stops on this error. The Javadoc tool does do some primitive checking of doc comments. Use the DocCheck doclet to check the doc comments more thoroughly.
When the Javadoc tool builds its internal structure for the documentation, it loads all referenced classes. Because of this, the Javadoc tool must be able to find all referenced classes, whether bootstrap classes, extensions, or user classes. For more about this, see How Classes Are Found. Generally speaking, classes you create must either be loaded as an extension or in the Javadoc tool's class path.
When a custom doclet is not specified with the -doclet command line option, the Javadoc tool uses the default standard doclet. The Javadoc tool has several command line options that are available regardless of which doclet is being used. The standard doclet adds a supplementary set of command line options. Both sets of options are described below in the options section.
To create a package comment file, you must name it package.html and place it in the package directory in the source tree along with the .java files. The Javadoc tool will automatically look for this filename in this location. Notice that the filename is identical for all packages. For explicit details, see the example of package.html.
The content of the package comment file is one big documentation comment, written in HTML, like all other comments, with one exception: The documentation comment should not include the comment separators /** and */ or leading asterisks. When writing the comment, you should make the first sentence a summary about the package, and not put a title or any other text between <body> and the first sentence. You can include package tags; as with any documentation comment, all tags except {@link} must appear after the description. If you add a @see tag in a package comment file, it must have a fully qualified name.
When the Javadoc tool runs, it automatically looks for this file; if found, the Javadoc tool does the following:
To create an overview comment file, you can name the file anything you want, typically overview.html, and place it anywhere, typically at the top level of the source tree. Notice that you can have multiple overview comment files for the same set of source files, in case you want to run javadoc multiple times on different sets of packages. For example, if the source files for the java.applet package are contained in /home/user/src/java/applet directory, you could create an overview comment file at /home/user/src/overview.html.
The content of the overview comment file is one big documentation comment, written in HTML, like the package comment file described previously. See that description for details. To reiterate, when writing the comment, you should make the first sentence a summary about the application or set of packages, and not put a title or any other text between <body> and the first sentence. You can include overview tags; as with any documentation comment, all tags except {@link} must appear after the description. If you add a @see tag, it must have a fully-qualified name.
When you run the Javadoc tool, you specify the overview comment file name with the -overview option. The file is then processed, similar to that of a package comment file:
To include unprocessed files, put them in a directory called doc-files, which can be a subdirectory of any package directory. You can have one such subdirectory for each package. You might include images, example code, source files, .class files, applets, and HTML files. For example, if you want to include the image of a button button.gif in the java.awt.Button class documentation, you place that file in the /home/user/src/java/awt/doc-files/ directory. Notice the doc-files directory should not be located at /home/user/src/java/doc-files because java is not a package - that is, it does not directly contain any source files.
All links to these unprocessed files must be hard-coded, because the Javadoc tool does not look at the files - it simply copies the directory and all its contents to the destination. For example, the link in the Button.java doc comment might look like:
/** * This button looks like this: * <img src="doc-files/Button.gif"> */
If you are unfamiliar with HTML frames, you should be aware that frames can have focus for printing and scrolling. To give a frame focus, click on it. Then, on many browsers the arrow keys and page keys will scroll that frame, and the print menu command will print it.
------------ ------------
|C| Detail | |P| Detail |
| | | | | |
| | | |-| |
| | | |C| |
| | | | | |
| | | | | |
------------ ------------
javadoc *.java javadoc java.lang java.awt
Load one of the following two files as the starting page depending on whether you want HTML frames or not:
For example, the document generated for the class java.applet.Applet would be located at java/applet/Applet.html. The file structure for the java.applet package follows, given that the destination directory is named apidocs. All files that contain the word "frame" appear in the upper-left or lower-left frames, as noted. All other HTML files appear in the right-hand frame.
NOTE: Directories are shown in bold. The asterisks (*) indicate the files and directories that are omitted when the arguments to javadoc are source file names (*.java) rather than package names. Also, when arguments are source file names, package-list is created but is empty. The doc-files directory is not created in the destination unless it exists in the source tree.
| apidocs | Top directory |
index.html | Initial page that sets up HTML frames |
| * overview-summary.html | Lists all packages with first sentence summaries |
overview-tree.html | Lists class hierarchy for all packages |
deprecated-list.html | Lists deprecated API for all packages |
constant-values.html Lists values of static fields for all packages | |
serialized-form.html | Lists serialized form for all packages |
| * overview-frame.html | Lists all packages, used in upper-left frame |
allclasses-frame.html | Lists all package classes, lower-left frame |
help-doc.html | Lists user help for how pages are organized |
index-all.html | Default index created w/o -splitindex option |
index-files | Directory created with -splitindex option |
index-<number>.html | Index files created with -splitindex option |
package-list | Lists package names used only for resolving external refs |
stylesheet.css | HTML style sheet for defining fonts, colors, positions |
java | Package directory |
applet | Subpackage directory |
Applet.html | Page for Applet class |
AppletContext.html | Page for AppletContext interface |
AppletStub.html | Page for AppletStub interface |
AudioClip.html | Page for AudioClip interface |
* package-summary.html | Lists classes with first sentence summaries |
* package-frame.html | Lists package classes, lower left-hand frame |
* package-tree.html | Lists class hierarchy for this package |
package-use | Lists where this package is used |
doc-files | Directory holding image & example files |
class-use | Directory holding pages API is used |
Applet.html | Page for uses of Applet class |
AppletContext.html | Page for uses of AppletContext interface |
AppletStub.html | Page for uses of AppletStub interface |
AudioClip.html | Page for uses of AudioClip interface |
src-html | Source code directory |
| java | |
public final class Boolean extends Object implements Serializable
and the declaration for the Boolean.valueOf method is:
public static Boolean valueOf(String s)
The Javadoc tool can include the modifiers public, protected, private, abstract, final, static, transient, and volatile, but not synchronized or native. These last two modifiers are considered implementation detail and not part of the API specification.
Rather than relying on the keyword synchronized, APIs should document their concurrency semantics in the comment description, as in "a single Enumeration cannot be used by multiple threads concurrently". The document should not describe how to achieve these semantics. As another example, while Hashtable should be thread-safe, there's no reason to specify that we achieve this by synchronizing all of its exported methods. We should reserve the right to synchronize internally at the bucket level, thus offering higher concurrency.
/** * This is the typical format of a simple documentation comment * that spans two lines. */
To save space you can put a comment on one line:
/** This comment takes up only one line. */
Placement of comments - Documentation comments are recognized only when placed immediately before class, interface, constructor, method, or field declarations (see the class example, method example, and field example). Documentation comments placed in the body of a method are ignored. Only one documentation comment per declaration statement is recognized by the Javadoc tool.
A common mistake is to put an import statement between the class comment and the class declaration. Avoid this, as javadoc will ignore the class comment.
/**
* This is the class comment for the class Whatever.
*/
import com.sun; // MISTAKE - Important not to put statement here
public class Whatever {
}
A comment is a description followed by tags - The description begins after the starting delimiter /** and continues until the tag section. The tag section starts with the first character @ that begins a line (ignoring leading asterisks and white space). It is possible to have a comment with only tags and no description. The description cannot continue after the tag section begins. The argument to a tag can span multiple lines. There can be any number of tags - some types of tags can be repeated while others cannot. This @see starts the tag section:
/** * This is a doc comment. * @see java.lang.Object */
Standard and in-line tags - A tag is a special keyword within a doc comment that the Javadoc tool can process. The Javadoc tool has standalone tags, which appear as @tag, and in-line tags, which appear within braces, as {@tag}. To be interpreted, a standalone tag must appear at the beginning of a line, ignoring leading asterisks, white space and comment separator (/**). This means you can use the @ character elsewhere in the text and it will not be interpreted as the start of a tag. If you want to start a line with the @ character and not have it be interpreted, use the HTML entity @. Each standalone tag has associated text, which includes any text following the tag up to, but not including, either the next tag, or the end of the doc comment. This associated text can span multiple lines. An in-line tag is allowed and interpreted anywhere that text is allowed. The following example contains the standalone tag @deprecated and in-line tag {@link}.
/**
* @deprecated As of JDk 1.1, replaced by {@link #setBounds(int,int,int,int)}
*/
Comments are written in HTML - The text must be written in HTML, in that they should use HTML entities and HTML tags. You can use whichever version of HTML your browser supports; we have written the standard doclet to generate HTML 3.2-compliant code elsewhere (outside of the documentation comments) with the inclusion of cascading style sheets and frames. (We preface each generated file with "HTML 4.0" because of the frame sets.)
For example, entities for the less-than (<) and greater-than (>) symbols should be written < and >. Likewise, the ampersand (&) should be written &. The bold HTML tag <b> is shown in the following example:
/** * This is a <b>doc</b> comment. * @see java.lang.Object */
Leading asterisks - When javadoc parses a doc comment, leading asterisk (*) characters on each line are discarded; blanks and tabs preceding the initial asterisk (*) characters are also discarded. If you omit the leading asterisk on a line, all leading white space is removed. Therefore, you should not omit leading asterisks if you want leading white space to be kept, such as when indenting sample code with the <pre> tag.
First sentence - The first sentence of each doc comment should be a summary sentence, containing a concise but complete description of the declared entity. This sentence ends at the first period that is followed by a blank, tab, or line terminator, or at the first standalone tag. The Javadoc tool copies this first sentence to the member summary at the top of the HTML page. See -breakiterator for a description of how we are planning in a future release to change the way the sentence break is determined.
Declaration with multiple fields - Java allows declaring multiple fields in a single statement, but this statement can have only one documentation comment, which is copied for all fields. Therefore, if you want individual documentation comments for each field, you must declare each field in a separate statement. For example, the following documentation comment doesn't make sense when written as a single declaration and would be better handled as two declarations:
/** * The horizontal and vertical distances of point (x,y) */ public int x, y; // Avoid this
The Javadoc tool generates the following documentation from the above code:
public int yThe horizontal and vertical distances of point (x,y).
Use header tags carefully - When writing documentation comments for members, it is best not to use HTML heading tags such as <H1> and <H2>, because the Javadoc tool creates an entire structured document and these structural tags can interfere with the formatting of the generated document. However, it is fine to use these headings in class and package comments to provide your own structure.
More specifically, when a @param tag for a particular parameter is missing, then the comment for that parameter is copied. When an @throws tag for a particular exception is missing, the @throws tag is copied only if that exception is declared.
This behavior contrasts with version 1.3 and earlier, where the presence of any description or tag would prevent all comments from being inherited.
Also of interest, is the inline tag {@inheritDoc} is present in a description or any tag, the corresponding description or tag is copied at that spot.
The overridden method must be a member of a documented class, and not an external referenced class for the doc comment to actually be available to copy.
Inheriting of comments occurs in three cases:
In the first two cases, for method overrides, the Javadoc tool generates a subheading "Overrides" in the documentation for the overriding method, with a link to the method it is overriding.
In the third case, when a method in a given class implements a method in an interface, the Javadoc tool generates a subheading "Specified by" in the documentation for the overriding method, with a link to the method it is implementing.
Algorithm for Inheriting Method Descriptions If a method does not have a doc comment, the Javadoc tool searches for an applicable comment using the following algorithm, which is designed to find the most specific applicable doc comment, giving preference to interfaces over superclasses:
a. If the superclass has a doc comment for this method, use it. b. If step 3a failed to find a doc comment, recursively apply this entire algorithm to the superclass.
Tags come in two types:
* Standalone tags - Can be placed only in the tag section that follows the desription. There tags are not set off with curly braces: @tag.
* Inline tags - Can be placed anywhere in the comments description or in the comments for standalone tags. Inline tags are set off with curly braces:{@tag}.
For information about tags we might introduce in future releases, see Proposed Tags.
The current tags are:
| ||||||||||||||||||||||||||||||||||||||||
For custom tags, see the -tag option.
/**
* @deprecated As of JDK 1.1, replaced by
* {@link #setBounds(int,int,int,int)}
*/
For more about deprecation, see the @deprecated tag.
This {@docRoot} tag can be used both on the command line and in a doc comment:
javadoc -bottom '<a href={@docRoot}/copyright.html>Copyright</a>'
NOTE - When using {@docRoot} this way in a make file, some makefile programs require special escaping for the brace {} characters. For example, the Inprise MAKE version 5.2 running on Windows requires double braces: {{@docRoot}}. It also requires double (rather than single) quotes to enclose arguments to options such as -bottom (with the quotes around the href argument omitted).
/**
* See the <a href="{@docRoot}/copyright.html">Copyright</a>.
*/
The reason this tag is needed is because the generated docs are in hierarchical directories, as deep as the number of subpackages. This expression:
<a href={@docRoot}/copyright.html>
would resolve to:
<a href="../../copyright.html"> ... for java/lang/Object.java
and
<a href="../../../copyright.html"> ... for java/lang/ref/Reference.java
This tag can be places in two positions:
This tag is very similar to @see - both require the same references and accept exactly the same syntax for package.class#member and label. The main difference is that {@link} generates an in-line link rather than placing the link in the "See Also" section. Also, the {@link} tag begins and ends with curly braces to separate it from the rest of the in-line text. If you need to use "}" inside the label, use the HTML entity notation }
There is no limit to the number of {@link} tags allowed in a sentence. You can use this tag in the description part of a documentation comment or in the text portion of any tag (such as @deprecated, @return, or @param).
For example, here is a comment that refers to the getComponentAt(int, int) method:
Use the {@link #getComponentAt(int, int) getComponentAt} method.
>From this, the standard doclet would generate the following HTML (assuming it refers to another class in the same package):
Use the
<a href="Component.html#getComponentAt(int, int)">\
getComponentAt</a>method.
which appears on the web page as:
Use the getComponentAt method.
You can extend {@link} to link to classes not being documented by using the -link otion.
Refer to {@linkplain add() the overridden method}.
This would display as
Refer to the overridden method.
@see "The Java Programming Language"
This generates text such as:
See Also:
"The Java Programming Language"
@see <a href="spec.html#section">Java Spec</a>
This generates a link such as:
See Also:
Java Spec
Example - In this example, an @see tag (in the Character class) refers to the equals method in the String class. The tag includes both arguments, that is, the name "String#equals(Object)" and the label "equals":
/**
* @see String#equals(Object) equals
*/
The standard doclet produces HTML something like this:
<dl>
<dt><b>See Also:</b>
<dd><a href="../../java/lang/String#equals\
(java.lang.Object)"><code>equals</code></a>
</dl>
The above looks something like this in a browser, where the label is the visible link text:
See Also:
equals
Specifying a Name - This package.class#member name can be either fully qualified, such as java.lang.String#toUpperCase(), or not, such as String#toUpperCase() or #toUpperCase(). If less than fully-qualified, the Javadoc tool uses the normal Java compiler search order to find it, further described below in Search order for @see. The name can contain whitespace within parentheses, such as between method arguments.
Of course the advantage to providing shorter, "partially-qualified" names is that they are less to type and less clutter in the source code. The following table shows the different forms of the name, where Class can be a class or interface, Type can be a class, interface, array, or primitive, and method can be a method or constructor.
|
The following notes apply to the above table:
Search Order for @see: The Javadoc tool will process an @see tag that appears in a source file (.java), package file (package.html), or overview file (overview.html). In the latter two files, you must fully qualify the name you supply with @see. In a source file, you can specify a name that is fully qualified or partially qualified.
When the Javadoc tool encounters an @see tag in a .java file that is not fully qualified, it searches for the specified name in the same order as the Java compiler would (except the Javadoc tool will not detect certain namespace ambiguities, since it assumes the source code is free of these errors). This search order is formally defined in Chapter 6, "Names" of the Java Language Specification, Second Edition. The Javadoc tool searches for that name through all related and imported classes and packages. In particular, it searches in this order:
The Javadoc tool continues to search recursively through steps 1-3 for each class it encounters until it finds a match. That is, after it searches through the current class and its enclosing class E, it searches through E's superclasses before E's enclosing classes. In steps 4 and 5, the Javadoc tool does not search classes or interfaces within a package in any specified order (that order depends on the particular compiler). In step 5, the Javadoc tool will look in java.lang,sincethatisautomatically imported by all programs.
The Javadoc tool won't necessarily look in subclasses, nor will it look in other packages even if their documentation is being generated in the same run. For example, if the @see tag is in java.awt.event.KeyEvent class and refers to a name in the java.awt package, javadoc will not look in that package unless that class imports it.
How a Name is Displayed - If label is omitted, then package.class.member will appear. In general, it will be suitably shortened relative to the current class and package. By "shortened", we mean the Javadoc tool will display only the minimum name necessary. For example, if the String.toUpperCase() method contains references to a member of the same class and to a member of a different class, the class name will be displayed only in the latter case:
Type of Ref- erence | Example | Displays As |
| @see tag refers to member of the same class | @see String#toLowerCase() |
toLowerCase()
(omits the class name)
|
| @see tag refers to member of a differ- ent class | @see Character#toLowerCase(char) |
Character.toLowerCase(char)
(includes the class name)
|
Examples of @see: The comment to the right shows how the name would be displayed if the @see tag is in a class in another package, such as java.applet.Applet:
| Example | See also: |
| @see java.lang.String | // String |
| @see java.lang.String The String class | // The String class |
| @see String | // String |
| @see String#equals(Object) | // String.equals(Object) |
| @see String#equals |
// String.equals\
(java.lang.Object)
|
| @see java.lang.Object#wait(long) |
// java.lang.Object.\
wait(long)
|
| @see Character#MAX_RADIX | // Character.MAX_RADIX |
| @see <a href="spec.html">Java Spec</a> | // Java Spec |
| @see "The Java Programming Language" |
// "The Java Programming \
Language"
|
@since 1.4
For source code in the Java platform, this tag indicated the version of the Java platform API specification (not necessarily when it was added to the reference implementation).
An optional field-description augments the doc comment for the field. The combined description must explain the meaning of the field and list the acceptable values. If needed, the description can span multiple lines. The standard doclet adds this information to the serialized form page.
The include and exclude arguments identify whether a class or package should be included or excluded from the serialized form page. They work as follows:
Examples: The javax.swing package is marked @serial exclude (in package.html). The public class java.sercurity.BasicPermission is marked @serial exclude. The package-private class java.util.PropertyPermissionCollection is marked @serial include.
The tag @serial at a class level overrides @serial at a package level.
For more information about how to use these tags, along with an example, see "Documenting Serializable Fields and Data for a Class," Section 1.6 of the Java Object Serialization Specification. Also see the "Serialization FAQ," which covers the questions, "Why do I see javadoc warnings stating that I am missing @serial tags? for private fields if I am not running javadoc with the -private switch?"
NOTE: The {@link} tag has a bug in overview documents in version 1.2. Text appears properly but has no link. The {@docRoot} tag foes not currently work in overview documents.
|
|
|
An example of a class comment:
/**
* A class representing a window on the screen.
* For example:
* <pre>
* Window win = new Window(parent);
* win.show();
* </pre>
*
* @author Sami Shaio
* @version 1.6, 01/23/03
* @see java.awt.BaseWindow
* @see java.awt.Button
*/
class Window extends BaseWindow {
}
|
An example of a field comment:
/** * The X-coordinate of the component. * * @see #getLocation() */ int x = 1263732;
|
An example of a method doc comment:
/**
* Returns the character at the specified index. An index
* ranges from <code>0</code> to <code>length() - 1</code>.
*
* @param index the index of the desired character.
* @return the desired character.
* @exception StringIndexOutOfRangeException
* if the index is not in the range <code>0</code>
* to <code>length()-1</code>.
* @see java.lang.Character#charValue()
*/
public char charAt(int index) {
}
The options are:
| -1.1 | -header | -package |
| -author | -help | -private |
| -bootclasspath | -helpfile | -protected |
| -bottom | -J | -public |
| -charset | -link | -quiet |
| -classpath | -linkoffline | -serialwarn |
| -d | -linksource | -source |
| -docencoding | -locale | -sourcepath |
| -doclet | -nodeprecated | -splitindex |
| -docletpath | -nodeprecatedlist | -stylesheetfile |
| -doctitle | -nohelp | -subpackages |
| -encoding | -noindex | -tag |
| -exclude | -nonavbar | -taglet |
| -excludedocfilessubdir | -noqualifier | -tagletpath |
| -extdirs | -nosince | -title |
| -footer | -notree | -use |
| -group | -overview | |
For information about the file specified by path/filename, see overview comment file.
Notice that the overview page is created only if you pass into javadoc two or more package names. For further explanation, see HTML Frames.
The title on the overview page is set by -doctitle .
For example, to call the MIF doclet, use:
-doclet com.sun.tools.doclets.mif.MIFDoclet
Example of path to jar file that contains the startig doclet class file. Notice the jar filename is included.
-docletpath /home/user/mifdoclet/lib/mifdoclet.jar
Example of path to starting doclet class file. Notice the class filename is omitted.
-docletpath /home/user/mifdoclet/classes/com/sun/tools/doclets/mif/
Note that you can use the -sourcepath option only when passing package names into the javadoc command - it will not locate .java files passed into the javadoc command. (To locate .java files, cd to that directory or include the path ahead of each file, as shown at Documenting One or More Classes.) If -sourcepath is omitted, javadoc uses the class path to find the source files (see -classpath). Therefore, the default -sourcepath is the value of class path. If -classpath is omitted and you are passing package names into javadoc, it looks in the current directory (and subdirectories) for the source files.
/home/user/src/com/mypackage/*.java
In this case, you would specify the source path to /home/user/src, the directory that contains com/mypackage, and then supply the package name com.mypackage:
example% javadoc -sourcepath /home/user/src/ com.mypackage
This is easy to remember by noticing that if you concatenate the value of the source path and the package name together and change the dot to a slash "/", you end up with the full path to the package:
example% javadoc -sourcepath /home/user/src/:/home/user2/src com.mypackage
For example, if you want to document com.mypackage, whose source files reside in the directory /home/user/src/com/mypackage, and if this package relies on a library in /home/user/lib, you would specify:
example% javadoc -classpath /home/user/lib -sourcepath \
/home/user/src com.mypackage
As with other tools, if you do not specify -classpath, the Javadoc tool uses the CLASSPATH environment variable, if it is set. If both are not set, the Javadoc tool searches for classes from the current directory.
For an in-depth description of how the Javadoc tool uses -classpath to find user classes as it relates to extension classes and bootstrap classes, see How Classes Are Found.
Specifying a locale causes javadoc to choose the resource files of that locale for messages (strings in the navigation bar, headings for lists and tables, help file contents, comments in stylesheet.css, and so forth). It also specifies the sorting order for lists sorted alphabetically, and the sentence separator to determine the end of the first sentence. It does not determine the locale of the doc comment text specified in the source files of the documented classes.
example% javadoc -J-Xmx32m -J-Xms32m com.mypackage
To tell what version of javadoc you are using, call the -version option of java:
example% javadoc -J-version java version "1.2" Classic VM (build JDK-1.2-V, green threads, sunwjit)
(The version number of the standard doclet appears in its output stream.)
For example, the following generates the documentation for the com.mypackage package and saves the results in the /home/user/doc/ directory:
example% javadoc -d /home/user/doc com.mypackage
Note that this documents only uses of the API, not the implementation. If a method uses String in its implementation but does not take a string as an argument or return a string, that is not considered a "use" of String.
You can access the generated "Use" page by first going to the class or package, then clicking on the "Use" link in the navigation bar.
example% javadoc -windowtitle "Java 2 Platform" com.mypackage
example% javadoc -doctitle "Java<sup><font size= TM</font></sup>" com.mypackage
You can use an absolute link for extdocURL to enable your docs to link to a document on any website, or can use a relative link to link only to a relative location. If relative, the value you pass in should be the relative path from the destination directory (specified with -d) to the diretory containing the packages being linked to.
When specifying an absolute link you normally use an http: link. However, if you want to link to a file system that has no web server, you can use a file: link - however, do this only if everyone wanting to access the generated documentation shares the same file system.
Choosing between -linkoffline and -link - One or the other option is appropriate when linking to an API document that is external to the current javadoc run.
Use -link: * when using a relative path to the external API document, or * when using an absolute URL to the external API document, if you shell does not allow a program to open a connection to that URL for reading.
Use -linkoffline: * when using an absolute URL to the external API document, if your shell does not allow a program to open a connection to that URL for reading. This can occur if you are behind a firewall and the document you want to link to is on the other side.
Example using absolute links to the external docs - Let's say you want to link to the java.lang, java.io and other Java 2 Platform packages at http://java.sun.com/j2se/1.4/docs/api. The following command generates documentation for the package com.mypackage with links to the Java 2 Platform packages. The generated documentation will contain links to the Object class, for example in the class trees. (Other options, such as -sourcepath and -d, are not shown.)
% javadoc -link http://java.sun.com/j2se/1.4/docs/api com.mypackage
Example using relative links to the external docs - Let's say you have two packages whose docs are generated in different runs of the Javadoc tool, and those docs are separated by a relative path. In this example, the packages are com.apipackage, and API, and com.spipackage, an SPI (Service Provide Interface). YOu want the documentation to reside in docs/api/com/apipackage and docs/spi/com/spipackage. Assuming the API package documentation is already generated, and that docs is the current directory, you would document the SPI package with links to the API documentation by running:
% javadoc -d ./spi -link ../api com.spipackage
Notice the -link argument is relative to the destination directory (docs/spi).
Details - The -link option enables you to link to classes referenced to by your code but not documented in the current javadoc run. For these links to go to valid pages, you must know where those HTML pages are located, and specify that location with extdocURL . This allows, for instance, third party documentation to link to java.* documentation on http://java.sun.com.
Omit the -link option for javadoc to create links only to API within the documentation it is generating in the current run. (Without the -link option, the Javadoc tool does not create links to documentation for external references, because it does not know if or where that documentation exists.
This option can create links in several places in the generated documentation.
Another use is for cross-links between sets of packages: Execute javadoc on one set of packages, then run javadoc again on another set of packages, creating links both ways between both sets. A third use is as a "hack" to update docs: Execute javadoc on a full set of packages, then run javadoc again on only the smaller set of changed packages, so that the updated files can be inserted back into the original set.
Bug Fix for Referenced Classes - In 1.4 the following bug has been fixed:
Link bug in 1.2 and 1.3 - When @see or {@link}
references an excluded class and -link is used,
an <A HREF> hyperlink is created only if the class is referenced
with in an import statement or in a declaration. References
in the body of methods, alone are inadequate. A workaround
was to include an explicit (not wildcard) import
statement for the referenced class.
An see or {@link} reference with -link is now enough to load the referenced class and enable a link to it. You can remove any import statements you had added as workarounds, which we had suggested you comment as follows:
import java.lang.SecurityManager; // workaround to force @see/@link\
hyperlink
Package List - The -link option requires that a file named package-list, which is generated by the Javadoc tool, exist at the URL you specify with -link. The package-list file is a simple text file that lists the names of packages documented at that location. In an earlier example, the Javadoc tool looks for a file named package-list at the given URL, reads in the package names and then links to those packages at that URL.
For example, the package list for the Java Platform v1.4 API is located at
and starts out as follows:
java.applet java.awt java.awt.color java.awt.datatransfer java.awt.dnd java.awt.event java.awt.font etc.
When javadoc is run without the -link option, when it encounters a name that belongs to an external referenced class, it prints the name with no link. However, when the -link option is used, the Javadoc tool searches the package-list file at the specified extdocURL location for that package name. If it finds the package name, it prefixes the name with extdocURL .
In order for there to be no broken links, all of the documentation for the external references must exist at the specified URLs. The Javadoc tool does not check that these pages exist, only that the package-list exists.
Multiple Links: - You can supply multiple-link options to link to any number of external generated documents. Javadoc 1.2 has a known bug that prevents you from supplying more than one -link command. This was fixed in 1.2.2.
Specify a different link option for each external document to link to:
example% javadoc -link extdocURL1 -link extdocURL2 ... \ -link extdocURLn com.mypackage
where extdocURL1, extdocURL2, ... extdocURLn point respectively to the roots of external documents, each of which contains a file named package-list.
Cross-links - Note that "bootstrapping" may be required when cross-linking two or more documents that have not been previously generated. In other words, if package-list does not exist for either document, when you run the Javadoc tool on the first document, the package-list does not yet exist for the second document. Therefore, to create the external links, you must rree-generate the first document after generating the second document.
In this case, the purpose of first generating a document is to create its package-list (or you can create it by hand if you are certain of the package names). Then generate the second document with its external links. The Javadoc tool prints a warning if a needed external package-list file does not exist. html.
More specifically, use -linkoffline is the external document's package-list file is not accessible or does not exist at the extdocURL location but does exist at a different location, which can be specified by packageListLoc (typically local). Thus, is extdocURL is accessible only on the World Wide Web, -linkoffline removes the constraint that the Javadoc tool have a web connection when generating the documentation. Examples are given below.
The -linkoffline option takes two arguments - the first for the string to be embedded in the <a href> links, the second telling it where to find package-list:
You can specify multiple -linkoffline options in a given javadoc run. (Prior to 1.2.2, it could be specified only once.)
Example using absolute links to the external docs - Let's say you want to link to the java.lang, java.io and other Java 2 Platform packages at http://java.sun.com/j2se/1.4/docs/api, but your shell does not have web access. You could open the package-list file in a browser at http://java.sun.com/j2se/1.4/docs/api/package-list, save it to a local directory, and point to this local copy with the second argument, packagelistLoc . In this example, the package list file has been saved to the current directory "." . The following command generates documentation for the package com.mypackage with links to the Java 2 Platform packages. The generated documentation will contain links to the Object class, for example, in the class trees. (Other necessary options, such as -sourcepath, are not shown.)
% javadoc -linkoffline http://java.sun.com/j2se/1.4/docs/api . com.mypackage
Example using relative links to the external docs - It's not very common to use -linkoffline with relative paths, for the simple reason that -link usually suffices. When using -linkoffline, the package-list file is generally local, and when using relative links, the file you are linking to is also generally local. So it is usually unnecessary to give a different path for the two arguments to -linkoffline . When the two arguments are identical, you can use -link . See the -link relative example.
Manually Creating A package-list File - If a package-list file does not yet exist, but you know what package names your document will link to, you can create your own copy of this file by hand and specify its path with packagelistLoc . An example would be the previous case where the package list for com.spipackage did not exist when com.apipackage was first generated. This technique is useful when you need to generate documentation that links to new external documentation whose package names you know, but which is not yet published. This is also a way of creating package-list files for packages generated with Javadoc 1.0 or 1.1, where package-list files were not generated. Likewise, two companies can share their unpublished package-list files, enabling them to release their cross-linked documentation simultaneously.
Linking to Multiple Documents - You can include -linkoffline once for each generated document you want to refer to (each option is shown on a separate line for clarity):
example% javadoc -linkoffline docURL1 packagelistURL1 \
-linkoffline docURL2 packagelistURL2 \
...
Updating Docs - Another use for the -linkoffline option is useful if your project has dozens or hundreds of packages; if you have already run javadoc on the entire tree; and now, in a separate run, you want to quickly make some small changes and re-run javadoc on just a small portion of the source tree. This is somewhat of a hack in that it works properly only if your changes are only to doc comments and not to signatures. If you were to add, remove, or change any signatures from the source code, then broken links could show up in the index, package tree, inherited member lists, use page, or other places.
First, you create a new destination directory (call it update) for this new small run. Set the first argument of -linkoffline to the current directory "." and set the second argument to the relative path to the original docs (call it html), where it can find package-list:
example% javadoc -d update -linkoffline . html com.mypackage
When javadoc is done, copy these generated files (not the overview or index) in update over the original files in
NOTE: If using an asterisk in a pattern or pattern list, the pattern list must be inside quotes, such as "java.lang*:java.util".
If you do not supply any -group option, all packages are placed in one group with the heading "Packages". If the all groups do not include all documented packages, any leftover packages appear in a separate group with the heading "Other Packages".
For example, the following option separates the four documented packages into core, extension, and other packages. Notice the trailing "dot" does not appear in "java.lang*"; including the dot, such as "java.lang.*", would omit the java.lang package:
example% javadoc -group "Core Packages" "java.lang*:java.util" \
-group "Extension Packages" "javax.*" \
java.lang java.lang.reflect java.util javax.servlet java.new
This results in the groupings:
Core Packages java.lang java.lang.reflect java.util Extension Packages javax.servlet Other Packages java.new
example% javadoc -helpfile /home/doc/myhelp.html java.awt
example% javadoc -stylesheetfile \
/home/user/mystylesheet.css com.mypackage
would insert the following line in the head of every generated page:
This META tag is described in the HTML standard (4197265 and 4137321).
The -tag option outputs the tag's heading taghead in bold, followed on the next line by the text from its single argument, as shown in the example below. Like any standalone tag, this argument's text can contain inline tags, which are also interpreted. The output is similar to standard one-argument tags, such as @return and @author.
Placement of tags - The Xaoptcmf part of the argument determines where in the source code the tag is allowed to be placed, and whether the tag can be disabled (using X). You can supply either a, to allow the tag in all places, or any combination of the other letters:
X (disable tag) a (all) o (overview) p (packages) t (types, that is classes and interfaces) c (constructors) m (methods) f (fields)
Examples of single tags - An example of a tag option for a tag that can be used anywhere in the source code is:
-tag todo:a:"To Do:"
If you wanted @todo to be used only with constructors, methods and fields, you would use:
-tag todo:cmf:"To Do:"
Notice the last colon (:) above is not a parameter separator, but is part of the heading text (as shown below). You would use either tag option for source code that contains the tag @todo, such as:
@todo The documentation for this method needs work.
This line would produce output something like:
To Do:
The documentation for this method needs work.
or, if you'd rather keep it simple:
-tag todo:X
The syntax -tag todo:X works even if @todo is defined by a taglet.
Order of tags - The order of the -tag (and -taglet) options determine the order the tags are output. You can mix the custom tags with the standard tags to intersperse them. The tag options for standard tags are placeholders only for determining the order -- they take only the standard tag's name. (Subheadings for standard tags cannot be altered.) This is illustrated in the following example.
If -tag is missing, then the position of -taglet determines its order. If they are both present, then whichever appears last on the command line determines its order. (This happens because the tags and taglets are processed in the order that they appear on the command line. For example, if -taglet and -tag both have the name "todo", the one that appears last on the command line will determine its order.
Example of a complete set of tags - This example inserts "To Do" after "Parameters" and before "Throws" in the output. By using "X", it also specifies that @example is a tag that might be encountered in the source code that should not be output during this run. Notice that if you use @argfile, you can put the tags on separate lines in an argument file like this (no line continuation characters needed):
When javadoc parses the doc comments, any tag encountered that is neither a standard tag nor passed in with -tag or -taglet is considered unknown, and a warning is thrown.
The standard tags are initially stored internally in a list in their default order. Whenever -tag options are used, those tags get appended to this list -- standard tags are moved from their default position. Therefore, if a -tag option is omitted for a standard tag, it remains in its default position.
Avoiding Conflicts - If you want to slice out your own namespace, you can use a dot-separated naming convention similar to that used for packages: com.mycompany.todo. Sun will continue to create standard tags whose names do not contain dots. Any tag you create will override the behavior of a tag by the same name defined by Sun. In other words, if you create a tag or taglet @todo, it will always have the same behavior you define, even if Sun later creates a standard tag of the same name.
You can also create more complex standalone tags, or custom inline tags with the -taglet option.
Taglets are useful for standalone or inline tags. They can have any number of arguments and implement custom behavior, such as making text bold, formatting bullets, writing out the text to a file, or starting other processes.
Use the -tagletpath option to specify the path to the taglet. Here is an example that inserts the "To Do" taglet after "Parameters" and ahead of "Throws" in the generated pages:
-taglet com.sun.tools.doclets.ToDoTaglet -tagletpath /home/taglets -tag return -tag param -tag todo -tag throws -tag see
Alternatively, you can use the -taglet option in place of its -tag option, but that may be harder to read.
% javadoc -d docs -sourcepath /home/user/src \
-subackages java:javax.swing
This command generates documentation for packages name "java" and "javax.swing" and all their subpackages.
There is also an option to exclude subpackages as it traverses the subpackages.
% javadoc -sourcepath /home/user/src -sourcepath java\
-exclude java.net:java.lang
would include java.io, java.util, and java.math (among others), but would exclude packages rooted at java.net and java.lang. Notice this excludes java.lang.ref, a subpackage of java.lang).
In 1.2 and 1.3, the java.text.BreakIterator class was used to determine the end of sentence for all languages but English. Therefore, the -breakiterator option has an effect only for English. English had its own algorithm, which looked for a period followed by a space. When -breakiterator is omitted, the end of the first sentence is unchanged from 1.2 and 1.3, and warnings are emitted displaying where there would be a difference.
Differences in the algorithms show up in English as follows:
The following example omits all package qualifiers:
-noqualifier all
The following example omits "java.lang" and "java.io" package qualifiers:
-noqualifier java.lang:java.io
The following example omits package qualifiers starting with "java", and "com.sun" subpackages (but not "javax"):
-noqualifier java.*:com.sun.*
Where a package qualifier would appear due to the above behavior, the following behavior (present in 1.3) also takes effect to further omit qualifiers: On the page for class p.C, remove the package qualifier for classes belonging to package p. This rule is present whether or not -noqualifier is used.
An argument file can include Javadoc options, source filenames and package names in any combination, or just arguments to Javadoc options. The arguments within a file can be space-separated or newline-separated. Filenames within an argument file are relative to the current directory, not the location of the argument file. Wildcards (*) are not allowed in these lists (such as for specifying *.java). Use of the '@' character to recursively interpret files is not supported. The -J options are not supported because they are passed to the launcher, which does not support argument files.
When executing javadoc, pass in the path and name of each argument file with the '@' leading character. When javadoc encounters an argument beginning with the character '@', it expands the contents of that file in the argument list.
Example - Single Arg File
You could use a single argument file named "argfile" to hold all Javadoc arguments:
% javadoc @argfile
This argument file could contain the contents of both files shown in the next example.
Example - Two Arg Files
You can create two argument files - one for the Javadoc options and the other for the package names or source filenames: (Notice the following lists have no line-continuation characters.)
Create a file named "options" containing:
-d docs-filelist
-use
-splitindex
-windowtitle 'Java 2 Platform v1.3 API Specification'
-doctitle 'Java<sup><font size="-2">TM</font></sup> 2\
Platform v1.4 API Specification'
-header '<b>Java 2 Platform </b><br><font size="-1">v1.4</font>'
-bottom 'Copyright 1993-2000 Sun Microsystems, Inc. All Rights Reserved.'
-group "Core Packages" "java.*"
-overview /java/pubs/ws/1.3/src/share/classes/overview-core.html
-sourcepath /java/pubs/ws/1.3/src/share/classes
Create a file named "packages" containing:
com.mypackage1 com.mypackage2 com.mypackage3
You would then run javadoc with:
% javadoc @options @packages
Example - Arg Files with Paths
The argument files can have paths, but any filenames inside the files are relative to the current working directory (not path1 or path2):
% javadoc @path1/options @path2/packages
Examples - Option Arguments
Here's an example of saving just an argument to a javadoc option in an argument file. We'll use the -bottom option, since it can have a lengthy argument. You could create a file named "bottom" containing its text argument:
Submit a bug or feature</a><br><br>Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.<br>Copyright 1993-2000 Sun Microsystems, Inc. 901 San Antonio Road,<br>Palo Alto, California, 94303, U.S.A. All Rights Reserved.</font>'
The run the Javadoc tool with:
% javadoc -bottom @bottom @packages
Or you could include the -bottom option at the start of the argument file, and then just run it as:
% javadoc @bottom @packages
The version number of javadoc can be determined using javadoc-J-version. The version number of the standard doclet appears in its output stream. It can be turned off with -quiet .
Public programmatic interface -
To invoke the
Javadoc
tool from within programs written in the
Java language. This interface is in
om.sun.tools.javadoc.Main
(and javadoc is re-entrant). For
more details, see Standard Doclet.
You can run javadoc either by changing directories (with cd) or by using -sourcepath option. The examples below illustrate both alternatives.
% javadoc -d /home/html -sourcepath /home/src\
-subpackages java -exclude java.net:java.lang
To also traverse down other package trees, append their names to the -subpackages argument, such as java:javax:org.xml.sax.
example% cd /home/src/ example% javadoc -d /home/html java.awt java.awt.event
example% javadoc -d /home/html -sourcepath /home/src \
java.awt java.awt.event
% javadoc -d /home/html -sourcepath\
/home/src1:/home/src2 java.awt java.awt.event
Result: All cases generate HTML-formatted documentation for the public and protected classes and interfaces in packages java.awt and java.awt.event and save the HTML files in the specified destination directory (/home/html). Because two or more packages are being generated, the document has three frames: for the list of packages, the list of classes, and the main page.
example% cd /home/src/java/awt
example% javadoc -d /home/html Button.java Canvas.java \
Graphics*.java
example% cd /home/src/
example% javadoc -d /home/html java/awt/Button.java \
java/applet/Applet.java
example% javadoc -d /home/html /home/src/java/awt/Button.java \
/home/src/java/awt/Graphics*.java
example% javadoc -d /home/html -sourcepath /home/src java.awt \
/home/src/java/applet/Applet.java
This example generates HTML-formatted documentation for the package java.awt and class Applet. The Javadoc tool determines the package name for Applet from the package declaration, if any, in the Applet.java source file.)
The same example is shown twice - first as executed on the command line, then as execued from a makefile. It uses absolute paths in the option arguments, which enables the same javadoc command to be run from any directory.
example% javadoc -sourcepath /jdk/src/share/classes -overview /java/jdk/src/share/classes/overview.html -d /jdk/build/api-use-splitIndex-windowtitle 'Java 2 Platform v1.2 API Specification' -doctitle 'Java<sup><font size="-2">TM</font></sup> 2 \
Platform v1.2 API Specification ' -header '<b>Java 2 Platform </b><br><font size="-1">v1.2</font>' -bottom '<font size="-1"><a href="http://java.sun.com/cgi-bin/bugreport.cgi"> \
Submit a bug or feature</a><br><br>Java is a trademark or registered \
trademark of Sun Microsystems,Inc. in the US and other countries.<br> \
Copyright 1993-1999 Sun Microsystems, Inc.
901 San Antonio Road,<br>Palo Alto, California, 94303, U.S.A. \
All Rights Reserved.</font>'
-group "Core Packages" "java.*:com.sun.java.*:org.omg.*" -group "Extension Packages" "javax.*" -J-Xmx180m @packages
where packages is the name of a file containing the packages to process, such as java.applet, java.lang. None of the options should contain any newline characters between the single quotes. (For example, if you copy and paste this example, delete the newline characters from the -bottom option.) See the other notes listed below.
example% javadoc -sourcepath /jdk/src/share/classes /* Sets path for
source files */
-overview $ (SRCDIR)/overview.html /* Sets path for overview text */
-d /jdk/build/api /* Sets destination directory */
-use /* Adds "Use" files */
-splitIndex /* Splits index A-Z */
-windowtitle $(WINDOWTITLE) /* Adds a window title */
-doctitle $(DOCTITLE) /* Adds a doc title */
-header $(HEADER) /* Adds running header text */
-bottom $(BOTTOM) /* Adds text at bottom */
-group $(GROUPCORE) /* 1st subhead on overview page */
-group $(GROUPEXT) /* 2nd subhead on overview page */
-J-Xmx180m /* Sets memory to 180MB */
java.lang java.lang.reflect /* Sets packages to document */
java.util java.io java.net java.applet
WINDOWTITLE = 'Java Platform 1.2 Final API Specification'
DOCTITLE = 'Java<sup><font size="-2">TM</font></sup> Platform 1.2 \
Final API Specification'
HEADER = '<b>Java Platform 1.2</b><br><font size="-1">Final</font>'
BOTTOM = '<font size="-1"><a href="http://java.sun.com/cgi-bin/\
bugreport.cgi">Submit a bug or feature</a><br><br>Java \
is a trademark or registered trademark of Sun \
Microsystems, Inc. in the US and other countries.<br>\
Copyright 1993-1998 Sun Microsystems, Inc. 901 San \
Antonio Road,<br>Palo Alto, California, 94303, U.S.A.\
</font>'
GROUPCORE = '"Core Packages" "java.*:com.sun.java.*:org.omg.*"
GROUPEXT = '"Extension Packages" "javax.*"'
SRCDIR = '/java/jdk/1.2/src/share/classes'
Single quotes are used to surround makefile arguments.
.:/home/classes:/usr/local/java/classes
javac(1), java(1), jdb(1), javah(1), javap(1)
See (or search java.sun.com) for the following: