ChatGPT解决这个技术问题 Extra ChatGPT

Eclipse how to link a jar containing javadocs/source with its binary?

I would like to have the javadoc comments contained in a jar file show in eclipse when I hover over a class.

For example, after downloading JODA-2.0, three jars are obtained:

joda-time-2.0

joda-time-2.0-javadoc

joda-time-2.0-sources (http://sourceforge.net/projects/joda-time/files/joda-time/2.0/joda-time-2.0-dist.zip/download)

In eclipse, [right click project -> Properties -> Java Build Path -> Libraries -> ADD JARs...] includes the binary in the project (can reference those classes) for joda-time-2.0.jar.

But how can I link in the sources/javadoc comments contained in the other two jar files so that when I hover over those classes I see the javadocs?

Adding the other two jars (joda-time-2.0-javadoc.jar and joda-time-2.0-sources.jar) to the build path does not link the javadocs or the source.


M
Mister Smith

Right click over the project -> Build path -> Configure build path In the new window, go to the "Libraries" tab. Select the library and expand it. There are 4 child options. Select "Javadoc location" and click the "Edit" button on the right. Now you can add a jar file containing the docs. This would be just a zip file with the extension changed to jar. Make sure inside the jar the index.html is in the root. You can press the "Validate" button to check everything is ok.


to 3. Select the library directly, not the one listed in Android Private Libraries (there is not edit functionality). To 4: Use Javadoc in Archives, not URL
Just to add that if you're using a common library you can attach a link to the API online instead of a hard-coded local path to the JAR file.
d
draganstankovic

You can try to CTRL + click on a class that has no source attached (do that in editor). When it shows you some info about the class you'll see the button that guides to attach source dialog. Click it and in dialog that pops up pick the source/javadoc location for your class.

You can also do that from project build path settings you are mentioning: pick libraries tab, expand the library (jar) you want and you'll be offered to pick: source attachment, javadoc attachment, native library location, etc. You just pick whatever you want and edit its current settings.

Or you can do as @JB Nizet said...


What's the difference between attaching source vs attaching javadoc?
@Pacerier source contains the program source code, where javadoc contains documentation and comments explaining the program's functionality.
I get the message "The JAR of this class file belongs to container 'Android Private libraries' wich does not allow modifications to source attachments on its entries
J
JB Nizet

Right-click on the jar (the one with the class files), choose "Properties", the "Javadoc location", and choose the jar file with the javadoc. Do similarly for the source jar, using "Java source attachment".


It might just be me but this only works if Eclipse "recognize" the file as a jar. That it is it has a little jar icon next to it. When I import 3rd party jars they show up with what appears to be document icon. They work just fine code wise but right clicking properties I don't get "javadoc location".
If your jars are in WEB-INF/lib, you shouldn't right-click on them (as you said, they show up as documents) but from the equivalent inside "Web App Libraries"
M
Michael_S

You can also edit the classpathentry in the file ".classpath" in your eclipse project. This can be helpful if you want to modify many entries at once or if you want to generate the path. e.g.:

<classpathentry kind="var" path="HOME/3rdparty/junit.jar" sourcepath="/SOURCE/junit_src.zip"/>


+1 answer! I was thinking of such a method to add for various jars at once.
-1 You don't want to edit the .classpath file directly. It is intended to only be used by eclipse, not edited manually. @Mister Smith actually gives the real correct answer here. Though draganstankovic explains a method that will at least get the job done...
@scribaniwannabe: I know that this is neither the intended, supported way nor the easiest way, but for the use cases I mentioned it works much faster and without problems. However you have to be careful to use valid XML syntax.
d
dbc

The easiest way is :

Press control+(The word for which you want to see the Java doc) A page will appear that has a button for attach source.Click This Select External location. Browse The location to Java folder than your jdk .Than select src .zip. press OK.

The Problem is Solved.