理论上应该可以从Java程序本身运行javadoc生成过程(如此处所述http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/standard-doclet.html#runningprogrammatically)。我从here中取出了程序,然后稍微改了一下,如下所示。但是当我从我班级的主方法中调用doTest方法时,我总是得到返回代码" 1"来自执行函数,导致错误。
static void doTest() {
ByteArrayOutputStream redirectedOutput = new ByteArrayOutputStream();
PrintStream originalOutput = System.out;
// redirect System.out to a buffer
System.setOut(new PrintStream(redirectedOutput));
PrintWriter sink = new PrintWriter(new ByteArrayOutputStream());
// execute javadoc
int result = com.sun.tools.javadoc.Main
.execute(
"javadoc", sink, sink, sink,
"com.sun.tools.doclets.standard.Standard",
new String[] {
"-sourcepath",
"\"C:\\Program Files (x86)\\Java\\jdk1.7.0_51\\src\\\"",
"java.util" });
// test whether javadoc did any output to System.out
if (redirectedOutput.toByteArray().length > 0) {
originalOutput
.println("Test failed; here's what javadoc wrote on its standard output:");
originalOutput.println(redirectedOutput.toString());
throw new Error("javadoc output wasn\'t properly redirected");
} else if (result != 0) {
throw new Error("javadoc run failed " + result);
} else {
originalOutput.println("OK, good");
}
}
我可以使用以下行生成javadoc:
C:\Program Files (x86)\Java\jdk1.7.0_51\bin>javadoc -sourcepath ..\src\ -d "C:\Users\leo\Eigene Dateien\javadoc" java.util
我的环境是使用JDK 1.7.0_51的Windows。
答案 0 :(得分:1)
我执行的方式不同,请参阅我的问题 - Doclet: firstSentence is null
我说(没有测试),你的参数应该是这样的:
String[] params = new String[] {
"-doclet", "com.sun.tools.doclets.standard.Standard",
"-sourcepath", "C:\\Program Files (x86)\\Java\\jdk1.7.0_51\\src\\",
"java.util"
};
com.sun.tools.javadoc.Main.execute(params);