Java has upgraded its Javadoc
for better user interaction and user-experience. It includes a simplified search, HTML generator, Doclet API, etc.
Table of Contents
What is Javadoc
The Javadoc
is a tool like javac
and a part of JDK
. It is used to generate HTML documentation of Java source code. If you ever visit to oracle site you will see the Java documentation which is generated by Javadoc. The upgraded Javadoc has the following features.
Simplified Doclet API
In Java 9, old Doclet API
has been replaced with a new simplified API. Although the existing API is still supported and available but you can use the new Doclet API.
Support for HTML5
It supports the HTML5 for better user interface and tools. So the new Javadoc
is based on HTML5
version.
Improved Search
Javadoc provides a new search box
to find class, packages, methods. It provides better suggestion and accurate search in a moment.
Support for Module System
It helps to generate documentation based on new Java platform module system.
Let’s take an example.
How to create HTML Javadoc from Java source file.
Let’s create an example to generate new HTML5 documentation from a Java source file. Here, we created a Calculate class that contains some methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.java2blog; /** * It provides methods to compute basic arithmetic operations. * @author Irfan * @version 1.0 * */ public class Calculate { /** * Returns sum of two integer numbers * * @param a and b of integer type */ public int add(int a, int b){ return(a+b); } /** * Returns subtraction of two integer numbers * * @param a and b of integer type */ public int sub(int a, int b){ return(a-b); } /** * Returns multiply of two integer numbers * * @param a and b of integer type */ public long mult(int a, int b){ return(a*b); } /** * Returns division of two integer numbers * * @param a and b of integer type */ public float div(int a, int b){ return(a/b); } } |
Run Javadoc Command
To create Javadoc, use the following command. execute it using cmd
or terminal
after reaching to Java file location.
1 2 3 |
javadoc -d . -html5 Calculate.java |
It will generate populate some messages to the console which are processing instructions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Loading source file Calculate.java... Constructing Javadoc information... Standard Doclet version 11.0.8 Building tree for all the packages and classes... Generating ./com/java2blog/Calculate.html... Generating ./com/java2blog/package-summary.html... Generating ./com/java2blog/package-tree.html... Generating ./constant-values.html... Building index for all the packages and classes... Generating ./overview-tree.html... Generating ./index-all.html... Building index for all classes... Generating ./allclasses-index.html... Generating ./allpackages-index.html... Generating ./deprecated-list.html... Building index for all classes... Generating ./allclasses.html... Generating ./allclasses.html... Generating ./index.html... Generating ./help-doc.html... |
Now open the directory in which Calculate.java
file is stored. You will find several html
files there, open index.html
file into the browser and the page would be like this.
Click on the Calculate
class and you will see the class structure.
Scroll down the page and you will see the list of method belongs to Calculate
class.
And you can see a new search box
, search here with any text and you will get the suggestions.
Well, this is all about Javadoc
, It is easy and good tool to create a informative HTML doc using Source code.