The Javadoc tool and its options are used to generate HTML pages of API documentation from Java source files. Using our IDE, you can start a file server in the workspace and view the javadoc.
- Create your javadoc. Use a command like
javadoc -d docs *.javato generate a doc of all .java files in your workspace and store them in a folder nameddocs. - To view the file server created in the next step, enable webview in the workspace settings, then click Reload in the top right to display the webview tab. In these settings, you can also set Webview to open on run instead of console.
- Next, enter
python3 -m http.server 8080in the console tab to start the file server. There is also a setting to prevent "waiting for input" from appearing in the console that can be added to stop that message from appearing.
4. Switch to the Webview tab to view the file server running on port 8080.
- You will see the files in your workspace.
- Click on the
docsfolder, or wherever your javadoc is stored, to render the HTML pages.
Creating Javadocs and file server automatically on run
You can add Javadoc and server creation to the Java project so the webview displays the javadoc when the program is run. See step 2 above for additional notes.
First, add a custom run command in the template's workspace settings, like this one: javadoc -d docs *.java && /zyfiles/runjava.sh /usercode Main.java Main
Make sure to save the command. Use the format: javadoc -d docs *.java && /zyfiles/runjava.sh {{sourceFolder}} {{sourceFile}} {{sourceFileNoPathExt}}
Then, add this to the template's main method to start the server on run:
try {
JavadocServer.start(); }
catch(Exception e) {
System.out.println("Could not start Javadoc server.");
Finally, add a separate JavadocServer.java file to the template:
import com.sun.net.httpserver.HttpServer;
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.file.Files;
public class JavadocServer {
public static void start() throws IOException {
int port = 8080;
String rootDir = "docs";
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", exchange -> {
String path = exchange.getRequestURI().getPath();
if (path.equals("/")) path = "/index.html";
File file = new File(rootDir + path);
if (file.exists() && !file.isDirectory()) {
byte[] bytes = Files.readAllBytes(file.toPath());
exchange.getResponseHeaders().set("Content-Type", Files.probeContentType(file.toPath()));
exchange.sendResponseHeaders(200, bytes.length);
exchange.getResponseBody().write(bytes);
} else {
String response = "404 Not Found";
exchange.sendResponseHeaders(404, response.length());
exchange.getResponseBody().write(response.getBytes());
}
exchange.close();
});
server.setExecutor(null);
server.start();
System.out.println("Javadoc server started at http://localhost:8080/");
}
}
Learners can edit their main as needed as long as the JavadocServer statement is retained. Once a working template is created, it can be cloned elsewhere as needed using the clone from zyBook button in instructor created content, or the entire lab can be copied using clone.
If you want to avoid any additional code in learner's workspaces, follow the steps at the top. Webview should be added to workspace settings in the template (step 2) so workspaces are setup to view the javadoc in webview. Learners will need to complete the javadoc creation setup themselves (step 1) and instructors/TAs can start a server to render and view the javadoc.