IntelliJ IDEA 2023.2 Help

Javadocs

Javadoc comments are usually placed above classes, methods, or fields in your source code. A Javadoc provides a description of the code element located under it and contains block tags marked with @ with specific metadata.

You can generate an API reference for your project in HTML by using the Javadoc tool that comes with your JDK. IntelliJ IDEA provides integration with the tool and allows you to build reference guides right from the IDE.

Learn more about the correct format of Javadocs, style guide, terms and conventions from How to Write Doc Comments for the Javadoc Tool.

Add Javadocs

Add a Javadoc using automatic comments

For documentation comments, IntelliJ IDEA provides completion that is enabled by default.

  • Type /** before a declaration and press Enter. The IDE auto-completes the doc comment for you.

You can disable automatic insertion of Javadoc comments: in the Settings dialog Control+Alt+S, go to Editor | General | Smart Keys, and clear the Insert documentation comment stub checkbox.

Add a Javadoc using context actions

  • Place the caret at the declaration in the editor, press Alt+Enter, and select Add Javadoc from the list.

    Adding a Javadoc using the 'Add Javadoc' context action

In Kotlin, the @param and other tags are not generated because the recommended style requires incorporating the description of parameters and return values directly into the documentation comment.

Fix Javadocs

If a method signature has been changed, IntelliJ IDEA highlights the tag that doesn't match the method signature and suggests a quick-fix.

Fix using context actions

  • Place the caret at the tag, press Alt+Enter, and select an action. You can change the tag or delete it.

    Fix a Javadoc using context actions

Fix using the Fix doc comment action

You can also update an existing Javadoc comment to account for the changes in the declaration using the Fix doc comment action:

  1. Place the caret at the class, method, function, or a field, and press Control+Shift+A.

  2. Type fix doc comment and press Enter.

Render Javadocs

IntelliJ IDEA allows you to render Javadocs in the editor. Rendered comments are easier to read, and they don't overload your code with extra tags.

Click the Toggle Rendered View icon in the gutter next to the necessary documentation comment (or press Control+Alt+Q) to toggle the rendered view; click the Toggle Rendered View icon to edit the comment.

Javadocs in the editing mode
Javadocs in the rendered mode

Rendered Javadocs allow you to click links to go to the referenced web pages, or view quick documentation for the referenced topics.

To change the font size, right-click a Javadoc in the editor and select Adjust Font Size from the context menu. Note that the rendered comments use the same font size as the quick documentation popup.

Render Javadocs by default

You can configure the IDE to always render Javadocs in the editor.

  • Right-click the (the Toggle Rendered View icon icon in the gutter or the Toggle Rendered View icon) and enable the Render All option.

    Alternatively, in the Settings dialog Control+Alt+S, select Editor | General | Appearance and enable the Render documentation comments option.

To edit rendered Javadocs, click the the Toggle Rendered View icon icon in the gutter next to the comment.

Use custom tags in Javadocs

In Javadocs comments, you can use custom tags on top the predefined ones. Later on you can include them in your API reference guide.

Recognize custom tags

When you use a custom tag for the first time, the Javadoc declaration problems inspection highlights it in the editor as a wrong tag. To avoid that, add the tag to the list recognized tags.

  • Place the caret at your custom tag, press Alt+Enter, and select Add '@tagname' to custom tags.

    Recognize custom tags using context action
  • Alternatively, press Control+Alt+S to open the IDE settings, select Editor | Inspections. Locate the Javadoc declaration problems inspection in the list and add your tag to the Additional Javadoc tags list.

    Recognize custom tags in settings

Include custom tags in a Javadoc reference

To include your custom tags in an HTML Javadoc reference, add them as command-line arguments.

  1. Go to Tools | Generate JavaDoc and in the Command line arguments field, specify -tag tagname:Xaoptcmf:"taghead".

    Example: -tag location:a:"Development Location:"

    Xaoptcmf determines where in the source code the tag is allowed to be placed. You can use a to allow the tag in all places. Learn more about block tags in Javadocs from the Oracle documentation.

  2. Configure other options as described in Generate a Javadoc reference and generate the reference guide.

    The information from the tag is displayed on the corresponding pages.

    Custom tag generated

Generate a Javadoc reference

IntelliJ IDEA provides a utility that enables you to generate a Javadoc reference for your project.

  1. Go to Tools | Generate JavaDoc.

  2. In the dialog that opens, select a scope: a set of files or directories for which you want to generate the reference.

  3. In the Output directory, specify the folder to which the generated documentation will be placed.

  4. From the Visibility level list, select the visibility level of members that will be included in the generated documentation:

    • Private: include all classes and members. The level corresponds to the -private Javadoc parameter.

    • Package: include all classes and members except the private ones. The level corresponds to the -package Javadoc parameter.

    • Protected: include only public and protected classes and members. The level corresponds to the -protected Javadoc parameter.

    • Public: include only public classes and members. The level corresponds to the -public Javadoc parameter.

  5. You can specify a locale (for example en_US.UTF-8), command line arguments, and the maximum heap size.

  6. Click Generate to generate the reference.

Specify Generate JavaDoc Scope dialog

The Tools | Generate JavaDoc dialog invokes the Javadoc utility. The controls of the dialog correspond to the options and tags of this utility.

Item

Description

JavaDoc Scope

Use this area to specify the subset of files, folders, and packages for which Javadoc should be generated.

This scope can be the whole project, recently modified files, current file, custom scope, and so on.

Include test sources

Include documentation comments for test to the generated Javadoc.

Include JDK and library sources in -sourcepath

If this checkbox is selected, then paths to the JDK and library sources will be passed to the Javadoc utility. For more information, refer to documentation.

Link to JDK documentation (use -link option)

If this checkbox is selected, the references to the classes and packages from JDK will turn into links, which corresponds to using the -link option of the Javadoc utility.

This checkbox is only enabled when a link to the online documentation is specified in the Documentation Paths tab of the SDK settings.

For more information, refer to the Javadoc documentation.

Output directory

Specify the fully qualified path to the directory where the generated documentation will be stored. Type the path manually or click the Browse button and select the location in the dialog. The specified value is passed to the -d parameter of the Javadoc utility. If the specified directory does not exist in your system, you will be prompted to create it.

Visibility level

Specify the visibility level of members that you want to include in the generated documentation:

  • Private: include all classes and members. The level corresponds to the -private Javadoc parameter.

  • Package: include all classes and members except the private ones. The level corresponds to the -package Javadoc parameter.

  • Protected: include only public and protected classes and members. The level corresponds to the -protected Javadoc parameter.

  • Public: include only public classes and members. The level corresponds to the -public Javadoc parameter.

Generate hierarchy tree

Generate the class hierarchy. If this checkbox is cleared, the -notree parameter is passed to Javadoc.

Generate navigator bar

Generate the navigator bar. If this checkbox is cleared, the -nonavbar parameter is passed to Javadoc.

Generate index

Generate the documentation index. If this checkbox is cleared, the -noindex parameter is passed to Javadoc.

Separate index per letter

Generate a separate index file for each letter. If this checkbox is cleared, the -splitindex parameter is passed to Javadoc.

The checkbox is available only if the Generate index checkbox is selected.

@use

Document the use of the class and the package. When selected, the checkbox corresponds to the -use Javadoc parameter.

@author

Include the @author paragraphs. When selected, the checkbox corresponds to the -author Javadoc parameter.

@version

Include the @version paragraphs. When selected, the checkbox corresponds to the -version Javadoc parameter.

@deprecated

Include the @deprecated information. When the checkbox is cleared, the -nodeprecated parameter is passed to Javadoc.

Deprecated list

Generate the deprecated list. When the checkbox is cleared, the -nodeprecatedlist parameter is passed to Javadoc.

The checkbox is available only if the @deprecated checkbox is selected.

Locale

Type the desired locale.

Command line arguments

Type additional arguments to be passed to a Javadoc. Use the command line syntax.

Maximum heap size

Type the maximum heap size in Mb to be used by Java VM for running Javadoc.

Open generated documentation in browser

Automatically open the generated Javadoc in a browser.

Troubleshoot

javadoc: error – Malformed locale name: en_US.UTF-8

Clear the Locale field. In the Other command line arguments field, add -encoding utf8 -docencoding utf8 -charset utf8.

-encoding specifies the encoding of the source files. -docencoding specifies the encoding of the output HTML files and -charset is the charset specified in the HTML head section of the output files.

Reference: Javadoc code style

You can configure code style for your Javadocs. Press Control+Alt+S to open the IDE settings and then select Editor | Code Style | Java | Javadocs. Configure the options as necessary and use the right part of the dialog to preview the changes.

Learn how to reformat your code from Reformat code.

Item

Description

Alignment

In this area, define the way Javadoc comments should be aligned.

  • Align parameter descriptions: select this checkbox to have parameter descriptions aligned against the longest parameter name. Otherwise, the description is separated from the corresponding parameter name by a single space.

  • Align thrown exception descriptions: select this checkbox to have thrown exception descriptions aligned against the longest exception name. Otherwise, the description is separated from the exception name by a single space.

Blank lines

In this area, define where blank lines should be inserted in Javadoc comments.

  • After description: select this checkbox to have a blank line automatically inserted after the description section of a Javadoc comment.

  • After parameter descriptions: select this checkbox to have a blank line inserted after the group of @param tags.

  • After return tag: select this checkbox to have a blank line inserted after the @return tag.

Invalid tags

In this area, define whether invalid tags should be preserved or not.

  • Keep invalid tags: select this checkbox to have the @invalidTag preserved.

  • Keep empty @param tags: select this checkbox to have @param tags without description preserved.

  • Keep empty @return tags: select this checkbox to have @return tags without description preserved.

  • Keep empty @throws tags: select this checkbox to have @throws tags without description preserved.

Other

In this area, specify additional formatting options for Javadoc comments.

  • Enable leading asterisks: start each line of a Javadoc comment with an asterisk.

  • Use @throws rather than @exception: use the @throws tag.

  • Wrap at right margin: wrap the text that exceeds the right margin to the next line.

  • Generate "<p>" on empty lines: automatically insert the </p> tag on an empty line.

  • Keep empty lines: select this checkbox to have manually added empty lines preserved.

  • Do not wrap one line comments: keep short comments on one line with the opening and closing tags.

  • Preserve line feeds: if this checkbox is not selected (by default), line feeds are not preserved on reformatting. This is convenient when comments should be formatted within the boundaries of a paragraph, to occupy minimum space.

    If this checkbox is selected, line feeds will be preserved.

  • Parameter descriptions on new line: place the description of a Javadoc parameter (if any) to a new line. It uses the indent based on the continuation indent value.

  • Indent continuation lines: indent subsequent lines in multiline comments.

Productivity tips

View Javadocs in the editor

In IntelliJ IDEA, you can view external Javadocs for any symbol or method signature right from the editor. To be able to do that, configure library documentation paths or add downloaded documentation to the IDE.

  • Hover over the necessary symbol in the editor.

  • Place the caret at the symbol and press Control+Q (View | Quick Documentation).

    Press Control+Q again to open this documentation in the Documentation tool window.

Learn more from Quick Documentation

Last modified: 27 October 2023