Viewing code in a JAR file - java

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.

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

How to edit .java files imported in Eclipse from a .jar file

I have a question, perhaps it was already answered, but i didn't manage to find it and I appologize if the solution already exists (let me know if it is before deleting my thread).
Problem is:
I have created a program on another PC and exported it from eclipse as a .jar file. It works on my main PC when I double click on it but when I import it in Eclipse I can't find the .java file. So i can't edit it.
What I have done so far:
In eclipse I have created a new empty project
I have right clicked,import, archive file, selected the .class files that eclipse sees, but when I am in the Project Explorer in Eclipse I can't find the .java file where the main is. I mean I can click run as a program and it works, but there is no .java file, only .class files. What am I doing wrong?
That cranes.class should be cranes.java. At least on my other PC it is.
Program works fine, but I can't edit it on my main PC. What am I doing wrong?
Thanks and best regards
You need to select the Export Java source files and resources option while creating the jar file and then your Java files will be available on importing the project from the jar file.
This is similar to how you use other libraries. You depend on the Jar file which contains class bytecode (compiled) of java code. You can't edit any of such files directly in the project you are using it. Thought you can always extends functionalities in your current project using simple inheritance concepts.
If you think such functionalities are trivial you should prefer to change in the original project rebuild the jar and use the newer version of jar.
However if you feel similar things for 3rd party library you can
always make changes after taking fork from those library source
code (if open source) and build and use your own version or go
ahead and raise pull request if you are confident about your
changes.
Mostly when you build a jar file, all you have in it are .class files; these are the result of compiling .java files, and so are not editable with text editors.
You CAN create a jar file that contains .java (also known as source) files, and even a jar file that contains both .java and .class files, but if you ask eclipse to create a jar file, by default it is just going to put .class files and files from resource folders in it, not .java files.
Assuming from the question, the jar is a library created by OP, by compiling java files into class files and packing/exporting them. Although the class files can't be edited in any IDE, they can be de-compiled into Java files by using third-party applications.
I personally use IntelliJ for this de-compiling source files authored by me
Note: Although this gives OP the desired functionality, it may lead to violations if the classes are Copyrighted.
As IntelliJ states, they neither encourage nor discourage de-compiling class files and the decision is purely to the user's discretion.
EDIT: It is always recommended to use the original source files for editing. Try to host them on git so that it may be retrieved anytime required
It may be simpler to not use eclipse but jar/zip/tar your project directory on the one computer and simply extract it onto the other, then open that folder as a new project in Eclipse.
Best is the suggestion from #SanjayBharathi to use git and clone the repo on your other machine.

How do I import an external jar file and a separate file as a command line argument in Netbeans?

I'm having difficulty with my latest programming project. I have a program in Netbeans which relies upon an external library contained within a jar file. I am expected to use several imports to get the operations I need. While that's all fine and dandy, it also means I can't debug this code to make sure it works. I did see something on StackOverflow about doing a copy-pasta on the jar file into my code, but it uses so many commands and so much code that it would be more helpful to import the whole jar file, if possible.
How do I get Netbeans to recognize that I have an external jar file somewhere that it needs to read from in order to get its information?
Furthermore, I am expected to read in a file via the args[], and I am not sure how to read in command line arguments in Netbeans. How do I do this, and where am I expected to put the file to be read in?
I have figured out my issues and am going to answer this for future reference to anybody with this exact same problem. When you have a NetBeans project, you can open the project hierarchy and get a list of your source packages, test packages, libraries, and test libraries. The Libraries folder is where I needed the jar file.
I can right click on the libraries tab to add a jar/folder. Doing so gave me a file explorer to navigate to where I had my jar saved, and adding it fixed all the warnings for external library imports. The program now knows where to look for all the external libraries. Presumably, I'd have to repeat this if I ever moved the jar file.
As for adding the arguments, I found this under Run>Set Project Configuration>Customize. This brings up a window with the project properties. One of the text boxes will be for "Arguments," and filling this in with your commands will give you those commands as your Args[].

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?
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

Eclipse won't load my javadocs

I'm trying to create a SWT application in eclipse. I've followed the instructions and loaded the org.eclipse.swt project to my workspace, and made it a dependency of my project by adding it to my build path.
However i cannot see the javadoc when i'm referencing the SWT libraries. I tried extracting the 'src.zip' into the org.eclipse.swt project, and then tried generating the Javadoc which created a 'doc' folder with a whole bunch of html files, so i think it's all there, it's just that eclipse isn't picking it up.
Can someone suggest anything please?
You shouldn't need to generate anything to get the Javadoc.
See if you can edit your SWT build classpath entry and attach the src.zip there. That should be enough to display the associated javadoc (like in this tutorial, except you can reference an external file src.zip instead of the external 'src' directory).
You shouldn't need to extract the zip or generate the Javadoc. I assume you followed the instructions listed here:
Developing SWT applications using Eclipse
I followed the same steps and was able to view the Javadocs in the Javadoc view tab. The tab is automatically updated whenever you click on a class you are instantiating in your code.

Categories

Resources