如何使用Java程序读取Java文档注释?JAR文件场景需求咨询
Absolutely feasible! Let me walk you through how to pull this off—since standard java.lang.reflect alone doesn’t give you access to Javadoc comments (they aren’t stored in compiled .class files), but there are reliable workarounds and tools to make it happen.
Here are the most practical approaches to get those developer-written comments:
1. Use a Source-Code Parsing Library (Recommended)
Most production JARs don’t include source files by default, but if you can get your hands on the corresponding source JAR (usually available alongside the binary JAR in repositories like Maven Central), libraries like JavaParser can parse the source code directly to extract Javadoc.
Example with JavaParser
First, add the Maven dependencies (if using Maven):
<dependency> <groupId>com.github.javaparser</groupId> <artifactId>javaparser-core</artifactId> <version>3.25.8</version> </dependency> <dependency> <groupId>com.github.javaparser</groupId> <artifactId>javaparser-symbol-solver-core</artifactId> <version>3.25.8</version> </dependency>
Then, use this code to read Javadoc from a source JAR:
import com.github.javaparser.JavaParser; import com.github.javaparser.ParseResult; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver; import java.io.IOException; import java.nio.file.Paths; public class JarJavadocExtractor { public static void main(String[] args) throws IOException { // Path to your source JAR String sourceJarPath = "path/to/your-source.jar"; JarTypeSolver sourceJarSolver = new JarTypeSolver(sourceJarPath); JavaParser parser = new JavaParser(); parser.getParserConfiguration().setSymbolResolver(sourceJarSolver); // Target class to extract Javadoc from String targetClass = "com.example.MyClass"; ParseResult<CompilationUnit> parseResult = parser.parse(sourceJarSolver.getSourceForType(targetClass)); if (parseResult.isSuccessful()) { CompilationUnit unit = parseResult.getResult().get(); // Print class-level Javadoc unit.getJavadoc().ifPresent(javadoc -> System.out.println("Class Javadoc:\n" + javadoc.getContent()) ); // Print Javadoc for all methods unit.findAll(com.github.javaparser.ast.body.MethodDeclaration.class).forEach(method -> { method.getJavadoc().ifPresent(javadoc -> System.out.println("\nMethod " + method.getName() + ":\n" + javadoc.getContent()) ); }); } } }
2. Parse Generated Javadoc Output
If you have access to the original source code (not just the JAR), run the javadoc command to generate HTML or XML documentation, then parse that output to extract comments. This works but requires the raw source files, which isn’t always feasible.
Key Notes
- Source JAR Requirement: Binary JARs (the ones you typically use for execution) don’t include source code or Javadoc by default. You’ll need to obtain the matching source JAR for this to work.
- Alternative Libraries: If JavaParser isn’t your style, check out libraries like DocLava or use the JDK’s built-in
com.sun.javadocAPI (note: this is not part of the public standard API, so it may change between JDK versions).
So to answer your question directly: Yes, it’s totally possible to read and display Javadoc comments from a JAR—you just need the right tools and access to the source code (either via a source JAR or raw files).
内容的提问来源于stack exchange,提问作者Sohel Shaikh

