In Java programming, we can use the javadoc tool for generating API documentation from comments embedded in source code (Javadoc comments). However, it requires remembering various options of this tool, which is painful and time-consuming. Eclipse IDE can help to relieve this pain by providing the Javadoc Generation wizard. So this tutorial is going to walk you through the steps of generating Javadoc for a Java project using Eclipse. But first, let’s see some sample Javadoc comments:
/** * Connects and logins to the FTP server. * * @throws FTPConnectionException If an I/O error occurs. * @throws FTPLoginException If the login operation did not succeed. */ public void login() throws FTPConnectionException, FTPLoginException { // method body }
As we can see, the method login() has some comments beginning with /** and ending with */ - which make some Javadocs for the method (Javadoc comments). The generated documentation (in HTML format) for this method would look like the following screenshot:
Now, let’s see the steps in details.
In Eclipse IDE, select Generate Javadoc… from Project menu. The Javadoc Generation wizard appears as follows:
In this dialog, do the following steps (as marked by the red numbers in the above screenshot):
The next screen allows us to specify some arguments and options for the javadoc tool, such as document title (1); document structure (2); documenting tags (3); JAR files and projects to which referenced links are generated (4); and stylesheet for the document (5):
At least you should specify the document title, leave others as default, and then click Next.
In the next screen, we can optionally specify the overview document (1); VM options (2); Extra javadoc options (3):
If you want to reuse the settings for future exports (recommended), check the option “Save the settings of this Javadoc export as an Ant script” (4) and specify the location of the generated Ant build file. This option would be helpful as we tend to generate the Javadocs many times when the project evolves.
If the option “Open generated index file in browser” (5) is checked, then Eclipse will open the generated document in its internal web browser.
Click Finish, it may take a while for Eclipse to execute the javadoc tool to parse the source files and generate all the necessary stuffs, and we end up with a nice documentation as follows (sample):
NOTE: If an Ant build file is generated (when the option “Open generated index file in browser” is checked), then you can execute this Ant file to re-generate the Javadoc without walking through the wizard again. It’s time-saving.