| 1.0 | Introduction to Javadoc |
| 2.0 | |
| 3.0 | Javadoc Templates in Eclipse |
| 4.0 | Generate HTML Document |
| 5.0 | Resources |
Have you ever had an experience of reading your code months after you last worked on it? You probably had problems remembering exactly what the code does. We can actually do two things to avoid such a situation. One is to strive to make your program simple and readable. The other is to write good documentation. Javadoc is a convenient, standard way to document your Java code. Javadoc is actually a special format of comments. There are some utilities that read the comments, and then generate HTML document based on the comments. HTML files give us the convenience of hyperlinks from one document to another. Most class libraries, both commercial and open source, provide Javadoc documents. In this tutorial, we are going to learn several ways to write Javadoc comments with the assistance of Eclipse, and generate HTML files with the Javadoc Export WIzard. There are several tags for Javadoc comments. However, you don't have to try to memorize them all. Note: Most frequently used tags can be generated by the code template, and you can find others with Content Assist by pressing Ctrl-SPACE. |
There are two kinds of Javadoc comments: class-level comments, and member-level comments. Class-level comments provide the description of the classes, and member-level comments describe the purposes of the members. Both types of comments start with /** and end with */. For example, this is a Javadoc comment: /** This is a Javadoc comment */ 2.1 Class-level Comments Class-level comments provide a description of the class, and they are placed right above the code that declares the class. Class-level comments generally contain author tags, and a description of the class. An example class-level comment is below: /** 2.2 Member-level Comments Member-level comments describe the fields, methods, and constructors. Method and constructor comments may contain tags that describe the parameters of the method. Method comments may also contain return tags. An example of these member-level comments are below: /** 2.3 Javadoc Tags
|
Eclipse generates Javadoc everytime you create a class, method, or field, using templates built into the IDE. These templates can be modified to create Javadoc in the format that you want. Or you can turn off the Javadoc generation property. The Javadoc templates are found by selecting Window > Preferences. Under the tree on the left of the Preferences dialog, open up Java > Code Style > Code Templates. There is a check box at the bottom of the Code Templates page that says Automatically add comments for new methods and types. If you uncheck this box, Javadoc comments will not be generated. To configure generated comments, open the Comments tree. Select a type of comment to modify, and select Edit.... Text that looks like ${ } is a variable. Use the Insert Variable... button to see what variables are avaliable to you. |
|
4.1 Right click on the project and select Export.... Select Javadoc, and click Next. 4.2 Make sure the Javadoc command refers to the Javadoc command line tool. This tool is provided with JDK. The name is usually javadoc.exe. In the lab, you can find javadoc.exe in C:\j2sdk1.4.2\bin\ (that is, the Javadoc command is C:\j2sdk1.4.2\bin\javadoc.exe .) 4.3 Select the types that you want to create Javadoc for. 4.4 Make sure that the Use Standard Doclet radio button is chosen, and Browse for a destination folder. 4.5 Click Next for more options if you wish; otherwise click Finish. |