前提:需导入tools.jar
package com.xx; import com.sun.javadoc.ClassDoc; import com.sun.javadoc.MethodDoc; import com.sun.javadoc.RootDoc; public class JavaDocUse { private static RootDoc rootDoc; public static class Doclet { public static boolean start(RootDoc rootDoc) { JavaDocUse.rootDoc = rootDoc; return true; } } /** * 显示DocRoot中的基本信息 */ public static void show(){ ClassDoc[] classes = rootDoc.classes(); for(ClassDoc classDoc : classes){ System.out.println(classDoc.name()+' '+ "类的注释:"+classDoc.commentText()); MethodDoc[] methodDocs = classDoc.methods(); System.out.println("==========================方法分割线==========================="); for(MethodDoc methodDoc : methodDocs){ // 打印出方法上的注释 System.out.println("" +classDoc.name()+"," +methodDoc.name()+ "方法注释:" +methodDoc.commentText()); } } } public static void main(String[] args) { com.sun.tools.javadoc.Main.execute(new String[] {"-doclet", Doclet.class.getName(), "-encoding","utf-8","-classpath", "*", "D:\workplace\src\main\java\com\gcc\jvmc\api\client\supplier\service\xx.java"}); show(); } }