Why JAR Files Do Not Contain Documentation? - java

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?

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.

There is a separate Gradle task to generate javadoc. Try adding the following:
task javadocJar(type: Jar, dependsOn:javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir }
And then run:
gradle javadocJar
See if that helps.
In addition to the above, you can try and add the following to make to generate a single jar with both compiled classes and javadoc:
jar {
from javadoc.destinationDir
}
jar.dependsOn javadoc
I don't know if that's the right decision to bundle everything in the same jar. I prefer keeping the jars separate and maybe find another way to make the IDE use the javadoc jar file. Maybe try adding the javadoc jar as another dependency of the module.

Yes This is possible
Hi, This is possible but with a small change like in the jar file.
First of all, from a code point of view jar file contains only compiled ".class" files and not source files ".java"
So if you need a doc to be applied with a jar by this I mean not the index.html which gets created but the comment that appears whenever a person uses the jar API and calls a method with a suggestion.
Example :
For that, we need to also add a source file while generating .jar file.
Steps for the same:
Type comments/java docs in code
Generate Docs
This will create a doc folder in project folder
Now create jar file
Make sure you choose this option as shown below
Almost done just test it by importing jar to another project and it should the suggestions as per docs
Very Important this can be harmful as you are including source files.java in your jar so before making make sure if you need this or not.!!!!
Hope this gave your answer
Any questions you can contact me over: VaibhavMojidra.com

Related

Custom Javadoc not showing in IDE

I have built a library and linked it to my Eclipse project as an External JAR.
Though the library works well, I couldn't make the Javadoc I wrote appear, as Eclipse keeps showing me:
I've read through some similar posts and they all refer to the javadoc file location, but I have no separate file for Javadoc. Everything is written above fields and methods with basic annotations.
How am I supposed to "link" a javadoc that is already present in the library files ? Thank you.
You could export the jar, and in the export window you can mark the option export java source files and resources
I know this insn't the ideal fix(cause you would be exporting your source code) but it does work

is there a way to make changes in jar which doesnot have source code in it and rebuild it as a jar in eclipse?

Is there a way to make changes in jar which doesnot have source code in it and rebuild it with this change as a jar in eclipse.
You can look into using Java decompilers; in order to turn the .class files within the JAR archive back into .java source code. The process and some tools for that are outlined here.
But: understand that *decompiling" can be a tough business! Plus: there is always the question if the licence terms of the library you are using allows you to do that. Being able to do something isn't the same as being allowed to do that!
Finally: keep in mind that a JAR is just a deployment artifact. A lot of libraries are open source, and you can most often download that source and build the corresponding JARs completely on your own.

how to get java source code from war file?

I do not have the latest source code but have the war(up to date) file deployed on server.
Please suggest the best ways to
1) Retrieve source code from war/ear
2) Compare & Merge/update the available source code with the code present in war/ear but missing in available source code(I am using ECLIPSE IDE)
Thanks in advance
War files are basically zip files, so they are easy to extract. (using unzip or just renaming the file)
Next you could use a Java decompiler like JD.
But you won't get the original Java code as the compiler does a lot of optimization.
But it should give you a good starting point
Once you've extracted the classes from the EAR/WAR/Jars, use JAD to decompile the code you're interested in to get back to the source: http://varaneckas.com/jad/
I'm not sure there's any out-of-the-box tool that is going to compare/diff your original source with the decompiled source produced from something like JAD though. Also bear in mind, decompiling classes back to source is not going to produce source that looks identical to the original source - code style is going to be different, maybe even some structure of the code. It's going to be difficult to do a diff between the original source and decompiled source.
If you have the original source but not the source for the code that is currently deployed, maybe a better question is to ask 'why not'? If there's something missing in your build process where you are not tracking what source is being used for each build, maybe this is an easier issue to address moving forward, rather than trying to do something clumsy and error prone like a diff between some other source and decompiled source?
The exact answer: it is not possible to get the original source code (.java files) from a war as opposed to a jar (java archive). When you create a jar file, you can decide if you want to include the .java files. Only a java decompiler can help, see the other answers.
Using JD GUI you can the source code with java code, but you'll need to
Inside the war folder, under specific module - Based on your project hierarchy (if maven project -these config will be available in Pom.xml - it will define which path and what jar name)
you will have the Core JAR files of each module.
Open those jar files using any decompiler , you will be able to find the class/java files in it..
Here is your complete solution.
If while creating war file, you have to make sure that you have added the code.
Otherwise, do one thing.
Deploy the war file in your server, may be on tomcat server.
To deploy the war file, you need to put that war file in webapps folder (C:\ASHIS_CODE\apache-tomcat-9.0.65\webapps).
enter image description here
After putting, you need to restart your tomcat server.
Then one folder with same name as of your war file name, will be created in side webapps folder.
Open that folder in your eclipse or any other ide, that folder contains your project code.
** Hope this clears your issue.

How to customize "Overview" page in JavaDoc?

I'd like the "Overview" page of my library's javadoc to essentially be a user/API guide for the entire JAR. I know that adding a package-info.java class to a package allows you to write package-level javadocs, but what about at the jar-level?
I know I could put a README.md in my project's root, but I like to think of README's as a doc for library developers (that is, people who will be maintaining the library). But the JavaDocs are API guides for people who will be using the library.
You can create an overview HTML file and place it anywhere you like in your source tree. The convention is to call it overview.html and place it at the root of your tree, but you are certainly not obligated to do so. In fact, you can create multiple overview files for different purposes. When you generate your javadocs, you use the -overview flag and pass it the path to the target overview file.
You can find more information about overview file requirements here.
When you are using Maven and its plugin for JavaDoc:
Put a file named overview.html into folder src/main/javadoc and run the command mvn javadoc:javadoc; the JavaDoc files are written into folder target/site/apidocs.
This is also possible when you want to generate JavaDoc files for the Unit Tests: Put a file named overview.html into folder src/test/javadoc and run the command mvn javadoc:test-javadoc; the JavaDoc files are written into folder target/site/testapidocs.

Viewing code in a JAR file

I am a Java beginner, using Eclipse. I am trying to understand the NASA World Wind package. Specifically code that looks like this:
protected WorldWindow wwd;
protected StatusBar statusBar;
protected ToolTipController toolTipController;
protected HighlightController highlightController;
When I mouse over "WorldWindow" it says
Note: This element neither has attached source nor attached Javadoc
and hence no Javadoc could be found.
The code compiles and runs, though.
However, it points to a package in a JAR file. I can find the class it is referring to. But I cannot read the class file. When I double click on it, I get "Source not found" in the Class File Editor.
I want to understand how this code works. How can I read the class files within the JAR?
Reading the actual class files in the jar would require decompiling with a tool such as JD-GUI.
However a quick Google search returned the Javadocs for the entire library.
http://builds.worldwind.arc.nasa.gov/worldwind-releases/daily/docs/api/index.html
Enjoy!
Use a decompiler - try JD-Eclipse, an Eclipse plugin. After installing it, you should be able to click on the classes you want to view and be directed to decompiled code from within the JAR file.
The project you are using is open source, so you have a few options at this point.
Ideally you can associate the source code and javadoc with the .jar file in Eclipse. This will allow you to directly view (read only) the source and documentation for the library from within Eclipse. To do this, you will need to:
download the source code and/or the javadoc. These items may very well be included in the archive you used to originally download the project (either as a jar, zip, or expanded subdirectory).
inform Eclipse about the relationship between your compiled jar file and the source/javadoc. See this guide
☆ instant gratification ☆ View the source code directly from the project repository. The World Wind project appears to be accessible at http://worldwind31.arc.nasa.gov/svn/trunk/WorldWind/src/
You can decompile the compiled code. This solution is more useful if you're trying to investigate a closed source project or if you're debugging something unusual.
You have to "decompile" the class file into readable java code. There are several good decompilers out there, here is a very popular one JD-GUI.
Also if you are using an IDE you often can download the src files and attach them just to use as javadocs while writing your program.

Categories

Resources