You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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.

How to Extract Javadoc Comments from JAR Files in Java

Here are the most practical approaches to get those developer-written comments:

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.javadoc API (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:56:46