Here’s a high-level overview of how Java’s javadoc tool works and how you can generate & retrieve the resulting HTML pages:
1. Writing Javadoc comments
In your .java source files, you annotate classes, methods, fields, etc. with special /** … */ comments. The most common tags are:
/**
* Computes the factorial of a number.
*
* @param n the number to compute (must be ≥ 0)
* @return n! as a long
* @throws IllegalArgumentException if n < 0
*/
public static long factorial(int n) {
if (n < 0) throw new IllegalArgumentException("n must be non-negative");
long f = 1;
for (int i = 2; i <= n; i++) f *= i;
return f;
}
When you run the javadoc tool, it
- parses your source files
- extracts those /**…*/ blocks
- applies a “doclet” (by default, the standard HTML doclet)
- produces a set of interlinked HTML pages
2. Generating the HTML
A) From the command line
Suppose your sources live under src/ in package com.example.toolbox. You can run:
cd project-root
javadoc \
-d docs \ # output directory
-sourcepath src \
-subpackages com.example.toolbox
What this does:
-d docs tells Javadoc to write all HTML files under project-root/docs/
-sourcepath src tells it where to find your .java files
-subpackages … recursively picks up all packages under com.example.toolbox
After it finishes, you’ll see:
project-root/
└── docs/
├── index.html ← entry point
├── overview-summary.html
└── com/
└── example/
└── toolbox/
├── Toolbox.html
└── Utils.html
B) In a build tool
- Maven: use the maven-javadoc-plugin.
- Gradle: apply the
java plugin and call ./gradlew javadoc; it writes to build/docs/javadoc/.
3. Retrieving / Viewing the HTML
-
Local file system
Simply open docs/index.html (or build/docs/javadoc/index.html) in your browser.
e.g.
file:///path/to/project-root/docs/index.html
-
Serve over HTTP
If you want others on your network to see it, spin up a simple web server:
# Python 3
cd docs
python3 -m http.server 8000
# then visit http://localhost:8000/ in your browser
-
Linking to online Javadoc
When generating, you can link to external Javadoc (e.g. the standard Java SE API) so your docs can hyperlink to java.lang.String, etc.
javadoc \
-d docs \
-link https://docs.oracle.com/javase/8/docs/api/ \
-sourcepath src \
-subpackages com.example.toolbox
4. (Bonus) Advanced: Custom Doclets
- The Standard Doclet generates HTML5 by default (in JDK 11+).
- You can write your own by implementing the
jdk.javadoc.doclet.Doclet interface
→ produces PDF, Markdown, custom HTML, whatever you like.
Feel free to let me know if you’d like examples of:
- Customizing the look & feel (CSS, doclet flags)
- Programmatically invoking Javadoc from another Java program
- Embedding Javadoc into a JAR and serving it at runtime 😊