在用Eclipse写java代码时候,有时候因为不知道一个java函数的作用,会通过把鼠移动到java函数上,如果它有javadoc的相关内容就会显示出来。但是并非所有java代码都有javadoc;即使安装了javadoc,在eclipse中如果不进行设定,也可能无法使用。
我在win7下安装的是javase的jdk,发现eclipse中默认的javadoc路径是http://download.oracle.com/javase/7/docs/api/,显然这是一个在线资源,问题是网络总是不稳定的,有时候看不到对应的文档内容。so,what should we do?and what can we do?!
一个显而易见的解决方法是下载javadoc并在本地安装,然后在eclipse中修改javadoc的路径。但是,为什么要下载javadoc?不是有一个命令也叫javadoc的么?不能用么?在java安装路径下可以找到一个叫src.zip的压缩包,里面是java的源码,而且有注释,应该可以生成javadoc的。到oracle官方上看了一下,发现javase版本的jdk就是不带文档的,而javaee版本的jdk却带文档。好吧,反正我对oracle没有什么好感,对此我只能呵呵。
如何手动生成本地javadoc api文档?对于jdk中的java源码,因为包含太多的包,因此需要统计所有使用到的包,也就是“当前路径下包含java文件的所有文件夹”。ok,因为google不能用,百度出来的结果基本上是没参考价值,唯一有用的一个是分析swf源码的例子,够了。通过获取所有包的路径并存储在一个叫package.txt的文件中,并存储在java源码解压后的路径src下,然后运行cmd,cd到src路径,输入javadoc javadoc -d api @package.txt命令,静候5分钟,文档就会生成完毕!
然后我们也可以通过chrome打开,比如JComponent.html页面:
附:用来获取java源码中所有包含java文件的目录的代码:
package util; import java.io.File; import java.util.Vector; public class SniffDir { private String srcDir; private Vector<String> vs; public SniffDir(){ srcDir="D:/workspace/src"; vs=new Vector<String>(); } /** * Sniff函数, 其实就是一个DFS,深度优先搜索 * 用来获取src目录下的满足“当前路径下存在.java文件”这一条件的所有文件夹 * @param file */ public void Sniff(File file) { //File file=new File(curDir); File[] files=file.listFiles(); int len=files.length; boolean ok=false;//表示当前目录下是否有java文件,ok=true表示有并且记录过了。 for(int i=0; i<len; i++){ //System.out.println(files[i].toString()); if(files[i].isDirectory()){ Sniff(files[i]); }else if(files[i].isFile() && !ok){//files[i]为文件(肯定是java文件),并且还没有把当前路径放入向量vs中 ok=true; vs.addElement(file.toString().substring(srcDir.length()+1)); //System.out.println(files[i].toString()); } } } String sb=null; public void Print(){ int len=vs.size(); for(int i=0; i<len; i++){ sb=vs.get(i); sb=sb.replace('\\', '.'); System.out.println(sb); } } public static void main(String[] args){ SniffDir sd=new SniffDir(); sd.Sniff(new File(sd.srcDir)); sd.Print(); } }
获取的结果(存储在package.txt中并放到src目录下即可):
com.sun.corba.se.impl.activation com.sun.corba.se.impl.copyobject com.sun.corba.se.impl.corba com.sun.corba.se.impl.dynamicany com.sun.corba.se.impl.encoding com.sun.corba.se.impl.interceptors com.sun.corba.se.impl.io com.sun.corba.se.impl.ior com.sun.corba.se.impl.ior.iiop com.sun.corba.se.impl.javax.rmi.CORBA com.sun.corba.se.impl.javax.rmi com.sun.corba.se.impl.legacy.connection com.sun.corba.se.impl.logging com.sun.corba.se.impl.monitoring com.sun.corba.se.impl.naming.cosnaming com.sun.corba.se.impl.naming.namingutil com.sun.corba.se.impl.naming.pcosnaming com.sun.corba.se.impl.oa com.sun.corba.se.impl.oa.poa com.sun.corba.se.impl.oa.toa com.sun.corba.se.impl.orb com.sun.corba.se.impl.orbutil com.sun.corba.se.impl.orbutil.closure com.sun.corba.se.impl.orbutil.concurrent com.sun.corba.se.impl.orbutil.fsm com.sun.corba.se.impl.orbutil.graph com.sun.corba.se.impl.orbutil.threadpool com.sun.corba.se.impl.presentation.rmi com.sun.corba.se.impl.protocol com.sun.corba.se.impl.protocol.giopmsgheaders com.sun.corba.se.impl.resolver com.sun.corba.se.impl.transport com.sun.corba.se.impl.util com.sun.corba.se.internal.corba com.sun.corba.se.internal.CosNaming com.sun.corba.se.internal.iiop com.sun.corba.se.internal.Interceptors com.sun.corba.se.internal.POA com.sun.corba.se.org.omg.CORBA com.sun.corba.se.pept.broker com.sun.corba.se.pept.encoding com.sun.corba.se.pept.protocol com.sun.corba.se.pept.transport com.sun.corba.se.PortableActivationIDL com.sun.corba.se.PortableActivationIDL.InitialNameServicePackage com.sun.corba.se.PortableActivationIDL.LocatorPackage com.sun.corba.se.PortableActivationIDL.RepositoryPackage com.sun.corba.se.spi.activation com.sun.corba.se.spi.activation.InitialNameServicePackage com.sun.corba.se.spi.activation.LocatorPackage com.sun.corba.se.spi.activation.RepositoryPackage com.sun.corba.se.spi.copyobject com.sun.corba.se.spi.encoding com.sun.corba.se.spi.extension com.sun.corba.se.spi.ior com.sun.corba.se.spi.ior.iiop com.sun.corba.se.spi.legacy.connection com.sun.corba.se.spi.legacy.interceptor com.sun.corba.se.spi.logging com.sun.corba.se.spi.monitoring com.sun.corba.se.spi.oa com.sun.corba.se.spi.orb com.sun.corba.se.spi.orbutil.closure com.sun.corba.se.spi.orbutil.fsm com.sun.corba.se.spi.orbutil.proxy com.sun.corba.se.spi.orbutil.threadpool com.sun.corba.se.spi.presentation.rmi com.sun.corba.se.spi.protocol com.sun.corba.se.spi.resolver com.sun.corba.se.spi.servicecontext com.sun.corba.se.spi.transport com.sun.image.codec.jpeg com.sun.imageio.plugins.bmp com.sun.imageio.plugins.common com.sun.imageio.plugins.gif com.sun.imageio.plugins.jpeg com.sun.imageio.plugins.png com.sun.imageio.plugins.wbmp com.sun.imageio.spi com.sun.imageio.stream com.sun.java.swing com.sun.java.swing.plaf.gtk com.sun.java.swing.plaf.motif com.sun.java.swing.plaf.motif.resources com.sun.java.swing.plaf.nimbus com.sun.java.swing.plaf.windows com.sun.java.swing.plaf.windows.resources com.sun.javadoc com.sun.java_cup.internal.runtime com.sun.jmx.defaults com.sun.jmx.interceptor com.sun.jmx.mbeanserver com.sun.jmx.remote.internal com.sun.jmx.remote.protocol.iiop com.sun.jmx.remote.protocol.rmi com.sun.jmx.remote.security com.sun.jmx.remote.util com.sun.jmx.snmp.agent com.sun.jmx.snmp com.sun.jmx.snmp.daemon com.sun.jmx.snmp.defaults com.sun.jmx.snmp.internal com.sun.jmx.snmp.IPAcl com.sun.jmx.snmp.mpm com.sun.jmx.snmp.tasks com.sun.jmx.trace com.sun.mirror.apt com.sun.mirror.declaration com.sun.mirror.type com.sun.mirror.util com.sun.naming.internal com.sun.org.apache.bcel.internal.classfile com.sun.org.apache.bcel.internal com.sun.org.apache.bcel.internal.generic com.sun.org.apache.bcel.internal.util com.sun.org.apache.regexp.internal com.sun.org.apache.xalan.internal.extensions com.sun.org.apache.xalan.internal.lib com.sun.org.apache.xalan.internal.res com.sun.org.apache.xalan.internal.templates com.sun.org.apache.xalan.internal.utils com.sun.org.apache.xalan.internal com.sun.org.apache.xalan.internal.xslt com.sun.org.apache.xalan.internal.xsltc.cmdline com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt com.sun.org.apache.xalan.internal.xsltc com.sun.org.apache.xalan.internal.xsltc.compiler com.sun.org.apache.xalan.internal.xsltc.compiler.util com.sun.org.apache.xalan.internal.xsltc.dom com.sun.org.apache.xalan.internal.xsltc.runtime com.sun.org.apache.xalan.internal.xsltc.runtime.output com.sun.org.apache.xalan.internal.xsltc.trax com.sun.org.apache.xalan.internal.xsltc.util com.sun.org.apache.xerces.internal.dom com.sun.org.apache.xerces.internal.dom.events com.sun.org.apache.xerces.internal.impl com.sun.org.apache.xerces.internal.impl.dtd com.sun.org.apache.xerces.internal.impl.dtd.models com.sun.org.apache.xerces.internal.impl.dv com.sun.org.apache.xerces.internal.impl.dv.dtd com.sun.org.apache.xerces.internal.impl.dv.util com.sun.org.apache.xerces.internal.impl.dv.xs com.sun.org.apache.xerces.internal.impl.io com.sun.org.apache.xerces.internal.impl.msg com.sun.org.apache.xerces.internal.impl.validation com.sun.org.apache.xerces.internal.impl.xpath.regex com.sun.org.apache.xerces.internal.impl.xpath com.sun.org.apache.xerces.internal.impl.xs com.sun.org.apache.xerces.internal.impl.xs.identity com.sun.org.apache.xerces.internal.impl.xs.models com.sun.org.apache.xerces.internal.impl.xs.opti com.sun.org.apache.xerces.internal.impl.xs.traversers com.sun.org.apache.xerces.internal.impl.xs.util com.sun.org.apache.xerces.internal.jaxp.datatype com.sun.org.apache.xerces.internal.jaxp com.sun.org.apache.xerces.internal.jaxp.validation com.sun.org.apache.xerces.internal.parsers com.sun.org.apache.xerces.internal.util com.sun.org.apache.xerces.internal.utils com.sun.org.apache.xerces.internal.xinclude com.sun.org.apache.xerces.internal.xni com.sun.org.apache.xerces.internal.xni.grammars com.sun.org.apache.xerces.internal.xni.parser com.sun.org.apache.xerces.internal.xpointer com.sun.org.apache.xerces.internal.xs com.sun.org.apache.xerces.internal.xs.datatypes com.sun.org.apache.xml.internal.dtm com.sun.org.apache.xml.internal.dtm.ref com.sun.org.apache.xml.internal.dtm.ref.dom2dtm com.sun.org.apache.xml.internal.dtm.ref.sax2dtm com.sun.org.apache.xml.internal.res com.sun.org.apache.xml.internal.resolver com.sun.org.apache.xml.internal.resolver.helpers com.sun.org.apache.xml.internal.resolver.readers com.sun.org.apache.xml.internal.resolver.tools com.sun.org.apache.xml.internal.security.algorithms com.sun.org.apache.xml.internal.security.algorithms.implementations com.sun.org.apache.xml.internal.security.c14n com.sun.org.apache.xml.internal.security.c14n.helper com.sun.org.apache.xml.internal.security.c14n.implementations com.sun.org.apache.xml.internal.security.encryption com.sun.org.apache.xml.internal.security.exceptions com.sun.org.apache.xml.internal.security com.sun.org.apache.xml.internal.security.keys.content com.sun.org.apache.xml.internal.security.keys.content.keyvalues com.sun.org.apache.xml.internal.security.keys.content.x509 com.sun.org.apache.xml.internal.security.keys com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations com.sun.org.apache.xml.internal.security.keys.keyresolver com.sun.org.apache.xml.internal.security.keys.storage.implementations com.sun.org.apache.xml.internal.security.keys.storage com.sun.org.apache.xml.internal.security.signature com.sun.org.apache.xml.internal.security.transforms com.sun.org.apache.xml.internal.security.transforms.implementations com.sun.org.apache.xml.internal.security.transforms.params com.sun.org.apache.xml.internal.security.utils com.sun.org.apache.xml.internal.security.utils.resolver.implementations com.sun.org.apache.xml.internal.security.utils.resolver com.sun.org.apache.xml.internal.serialize com.sun.org.apache.xml.internal.serializer com.sun.org.apache.xml.internal.serializer.utils com.sun.org.apache.xml.internal.utils com.sun.org.apache.xml.internal.utils.res com.sun.org.apache.xpath.internal com.sun.org.apache.xpath.internal.axes com.sun.org.apache.xpath.internal.compiler com.sun.org.apache.xpath.internal.domapi com.sun.org.apache.xpath.internal.functions com.sun.org.apache.xpath.internal.jaxp com.sun.org.apache.xpath.internal.objects com.sun.org.apache.xpath.internal.operations com.sun.org.apache.xpath.internal.patterns com.sun.org.apache.xpath.internal.res com.sun.security.auth.callback com.sun.security.auth com.sun.security.auth.login com.sun.security.auth.module com.sun.security.jgss com.sun.source.tree com.sun.source.util java.applet java.awt java.awt.color java.awt.datatransfer java.awt.dnd java.awt.dnd.peer java.awt.event java.awt.font java.awt.geom java.awt.im java.awt.im.spi java.awt.image java.awt.image.renderable java.awt.peer java.awt.print java.beans java.beans.beancontext java.io java.lang java.lang.annotation java.lang.instrument java.lang.invoke java.lang.management java.lang.ref java.lang.reflect java.math java.net java.nio java.nio.channels java.nio.channels.spi java.nio.charset java.nio.charset.spi java.nio.file java.nio.file.attribute java.nio.file.spi java.rmi java.rmi.activation java.rmi.dgc java.rmi.registry java.rmi.server java.security java.security.acl java.security.cert java.security.interfaces java.security.spec java.sql java.text java.text.spi java.util java.util.concurrent java.util.concurrent.atomic java.util.concurrent.locks java.util.jar java.util.logging java.util.prefs java.util.regex java.util.spi java.util.zip javax.accessibility javax.annotation javax.annotation.processing javax.imageio.event javax.imageio javax.imageio.metadata javax.imageio.plugins.bmp javax.imageio.plugins.jpeg javax.imageio.spi javax.imageio.stream javax.lang.model.element javax.lang.model javax.lang.model.type javax.lang.model.util javax.management javax.management.loading javax.management.modelmbean javax.management.monitor javax.management.openmbean javax.management.relation javax.management.remote javax.management.remote.rmi javax.management.timer javax.naming javax.naming.directory javax.naming.event javax.naming.ldap javax.naming.spi javax.print.attribute javax.print.attribute.standard javax.print javax.print.event javax.rmi.CORBA javax.rmi javax.rmi.ssl javax.script javax.security.auth javax.security.auth.callback javax.security.auth.kerberos javax.security.auth.login javax.security.auth.spi javax.security.auth.x500 javax.security.cert javax.security.sasl javax.sound.midi javax.sound.midi.spi javax.sound.sampled javax.sound.sampled.spi javax.sql javax.sql.rowset javax.sql.rowset.serial javax.sql.rowset.spi javax.swing javax.swing.border javax.swing.colorchooser javax.swing.event javax.swing.filechooser javax.swing.plaf javax.swing.plaf.basic javax.swing.plaf.metal javax.swing.plaf.multi javax.swing.plaf.nimbus javax.swing.plaf.synth javax.swing.table javax.swing.text javax.swing.text.html javax.swing.text.html.parser javax.swing.text.rtf javax.swing.tree javax.swing.undo javax.tools javax.xml.bind.annotation.adapters javax.xml.bind.annotation javax.xml.bind.attachment javax.xml.bind javax.xml.bind.helpers javax.xml.bind.util javax.xml.crypto javax.xml.crypto.dom javax.xml.crypto.dsig javax.xml.crypto.dsig.dom javax.xml.crypto.dsig.keyinfo javax.xml.crypto.dsig.spec javax.xml.datatype javax.xml.namespace javax.xml.parsers javax.xml.soap javax.xml.stream javax.xml.stream.events javax.xml.stream.util javax.xml.transform.dom javax.xml.transform javax.xml.transform.sax javax.xml.transform.stax javax.xml.transform.stream javax.xml.validation javax.xml.ws javax.xml.ws.handler javax.xml.ws.handler.soap javax.xml.ws.http javax.xml.ws.soap javax.xml.ws.spi javax.xml.ws.spi.http javax.xml.ws.wsaddressing javax.xml javax.xml.xpath launcher org.ietf.jgss org.omg.CORBA org.omg.CORBA.DynAnyPackage org.omg.CORBA.ORBPackage org.omg.CORBA.portable org.omg.CORBA.TypeCodePackage org.omg.CORBA_2_3 org.omg.CORBA_2_3.portable org.omg.CosNaming org.omg.CosNaming.NamingContextExtPackage org.omg.CosNaming.NamingContextPackage org.omg.Dynamic org.omg.DynamicAny org.omg.DynamicAny.DynAnyFactoryPackage org.omg.DynamicAny.DynAnyPackage org.omg.IOP org.omg.IOP.CodecFactoryPackage org.omg.IOP.CodecPackage org.omg.Messaging org.omg.PortableInterceptor org.omg.PortableInterceptor.ORBInitInfoPackage org.omg.PortableServer org.omg.PortableServer.CurrentPackage org.omg.PortableServer.POAManagerPackage org.omg.PortableServer.POAPackage org.omg.PortableServer.portable org.omg.PortableServer.ServantLocatorPackage org.omg.SendingContext org.omg.stub.java.rmi org.w3c.dom org.w3c.dom.bootstrap org.w3c.dom.css org.w3c.dom.events org.w3c.dom.html org.w3c.dom.ls org.w3c.dom.ranges org.w3c.dom.stylesheets org.w3c.dom.traversal org.w3c.dom.views org.w3c.dom.xpath org.xml.sax org.xml.sax.ext org.xml.sax.helpers sunw.io sunw.util