从理论上讲,应该可以从 Java 程序本身运行 javadoc 生成过程(如此处所述http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/standard-doclet.html#runningprogrammatically) . 我从这里获取程序并稍微更改它,如下所示。但是,当我从类的 mainmethod 调用 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。