Skip to content

Why JAR Files Do Not Contain Documentation?

I’m in the process of writing a small Java library that contains a related code that I usually include in most of my android app. I decided to export the library as a jar file then drop the file in the libs folder of my future projects.

Using Android Studio:

  • I created a Java Library module and put my code in it. And I added some comments to some of the method, following this.
  • Then, I ran the jar task in gradle, which gave me the .jar file in build/libs directory of my module.

Now, when I used this jar in one of my android apps, Everything works as expected, except the Doc part. When I hover over the classes and methods of my library, I don’t see the Doc comments that I wrote.

Q1: Am I missing another step?
Q2: Are jar files supposed to have no comments?

Answer

The javadocs are the documents that are generated from the javadoc comments in your source code. They are not part of a normal JAR file because that would unnecessarily bloat the JAR files … with stuff that someone running to code doesn’t need.

The javadocs can be generated by a Gradle task, by the javadoc command (if you have a Java SDK installed) and by various other tools. You can then read them using a web browser.

On the other hand, IDEs can often render the javadoc comments in source code and display them as pop-ups, etcetera. (Some people would call this “javadocs”, but I think that is an overstatement, since you typically can’t navigate the documentation … like you can with read javadoc documents.)

In order to render the javadoc comments, the IDE needs the source code. JAR files don’t (normally) contain any source code or javadocs. Instead, the normal way to deal with this is to tell the IDE where the source code is, either by pointing it at a source code directory, a ZIP file containing source code, or URL for downloading the source code.

(I don’t use Android Studio, so I can tell you exactly how to do this. However, I imagine that the IDE’s online help explains how to do it …)


It seems that your end goal here is to distribute your libraries in a way that allows programmers to see the javadoc comments.

The simple way to do that is to distribute source code. This Q&A describes how to get Gradle to generate a separate archive containing the source code, or add the source code to the JAR containing your compiled code1.

If that isn’t acceptable, you may need to generate the javadocs as HTML2 and provide the HTML tree as a separate ZIP file that a programmer can unzip and read with a web browser. Alternatively, put the javadocs up on a website.


1 – I would not recommend this. People who just want to use the JAR as a binary are liable to complain about “bloat”.
2 – If neither providing source code or javadoc HTML documentation is acceptable, I don’t think there is a pragmatic solution.