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

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.

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.

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

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".

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"/>

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.

Related

How do I add .java/.class files to eclipse?

I know how to add .java/class files to a certain project, or import them into a package. So, when you're using eclipse you can import java.awt.; and java.util.; and the but those are all packages inside the JRE. So my real question is: is there any way to import a package containing the files i want into the JRE so that, if i create a new project, I can simply import them whenever I need them? Thanks!
Yes. Generally, you have more then one choice. You can link the project(s) in eclipse, and then have project B with a reference to project A. Or, you can create a build artifact like a jar file and add it to the project B build/run path. Simply add a jar file to your project, right click and select Add to Build Path. Another alternative is to use a build tool that manages dependencies like gradle, sbt, maven, or apache ivy. Note that there are a wide variety of third-party opensource libraries you can add as dependencies using these (and other) tools.
You seem to be asking how to create a shared library, which in Java is a .jar file. The steps to do this in Eclipse are:
In the Package Explorer, you can optionally pre-select one or more Java elements to export. (These will be automatically selected in the Opens the JAR export wizard JAR Package Specification wizard page, described in Step 4.)
Either from the context menu or from the menu bar's File menu, select Export.
Expand the Java node and select JAR file. Click Next.
In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field.
Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source files and resources. Note: Selected resources are exported in both cases.
In the Select the export destination field, either type or click Browse to select a location for the JAR file.
Select or clear the Compress the contents of the JAR file checkbox.
Select or clear the Overwrite existing files without warning checkbox. If you clear this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten.
Note: The overwrite option is applied when writing the JAR file, the JAR description, and the manifest file.
You have two options:
Click Finish to create the JAR file immediately.
Click Next to use the JAR Packaging Options page to set advanced options, create a JAR description, or change the default manifest.
Credit goes to the docs.

How to import a jar file into eclipse as a project to use source code of classes of that jar

I have one jar file. I want to see the source code of classes of this jar file and i need to use source code of this jar file. Is there any option to view and use that jar file source code?
Is there any option in eclipse to extract jar file and to see the source code of that jar file?
if you add a jar as dependency to your project, you can also link sourcecode to it.
Rightclick on project->Build Path->Configure Buildpath. There, choose the jar that you linked into your project, extend it and under "source attachments", you can add the jar that contains the sources of this specific jar
You can add the JAR file with the byte code and attach the source JAR as well. You can't do without the first; the second is optional.
It's easy to do in IntelliJ; I'm not sure about the steps in Eclipse.
Right click on Your procjet. Chose properties.
Select Libraries
Chose Your jar
Profit
The JAR file you are refering too may not have source code attached.
Usually, when downloading third party library you can download JARs with byte-code and source code separetredly. The best way to check is to simply extract JAR file (with ZIP extracting tool) and check whether you have source there. If so, you can do the following:
1. Add the JAR to your Eclipse project (just drag it there and choose whether you want to copy or link to the JAR).
2. Whenever you are opening class file that does not come with source-code and you are shown "Source not found" page - just press button "Attach Source.." and find your JAR file with source code.
[EDIT] use JD-GUI to extract source code (*.java files) from *.class files contained in JAR file.
In my case I had in Java Build Path Source attachment with meaningful path (not NONE), but file itself was absent in this place. I configure project to Maven nature. And then from Maven make Download sources - file -source.jar appears.
Yes,You can see the source code of all the files those are added in your jar file.first you have to add jar file same as shown by the Marcin Szymczak. and after that click on "Referenced library". After that click on Your jar.and you can check all the classes source code.In my case jar file-Hackerrank.jar and you can see source code of classes:
This applies to plugin jars (if that is your case):
Ensure to put the jar into your target platform folder
Refresh your target platform: Window -> Preferences -> Target Platform -> Select your current target -> Edit -> Finish -> Reload -> Ok
Open the "Plug-ins" view: Window -> Show view -> Plug-ins
In that view, locate the new plugin jar: right click and Import As -> Source Project.
You will have it as a plugin project in your workspace.

How to view Javadoc in Eclipse?

I am using a third party library, i have included into my project. It contains classes for the application, but no sources, or Javadoc. As expected, mouse-over imported object shows no javadoc.
Note: This element neither has attached source nor attached
Javadoc and hence no Javadoc could be found.
After googling for a bit, i came across 3 versions of jars
app.jar
app-sources.jar
app-javadoc.jar
It appears and app-javadoc.jar contains nothing but html pages and app-sources contans nothing but .java files (with Javadoc comments).
What i did:
Added all 3 files to the list of
"Referenced Libraries". After
"refresh"ing the project, javadoc
still does not show.
Additionally, under properties for
the project, i pointed "Javadoc
location path" to unzipped content of
app-javadoc.jar (validated ok)
Still, after having done all that, my Eclipse fails to display javadoc.
Please advise
Thank you.
Add only app.jar to the Referenced Libraries.
then, go to Project Explorer, right click on the jar file-> Properties -> Javadoc Location and add Javadoc jar location (app-javadoc.jar).
If you want to also browse the source code, then go to 'Java Source attachment' in the same dialog box and add the app-sources.jar there.
I think you should set the javadoc location to the library itself but not the Project's javadoc location property.
Project > Properties > Java Build Path > Libraries
expand the app.jar node and select the "Javadoc location" sub node and you can see the "Edit" button at the right of the library tree is enabled. Then click the "Edit" button and you can choose your javadoc in a jar file.
I am using Eclipse not MyEclipse but I think they are similiar. FYI
Here is an answer as of 2016-05-12:
In the Eclipse Project Explorer, under Referenced Libraries, right click on the jar file for which you require documentation. In the present example I require documentation for the Apache Commons IO library.
Select Properties. Then select Javadoc Location:
In the case that I am considering--the Apache Commons IO library--the javadoc jar file is stored in the same directory as the library jar file. In the Properties dialog, select Javadoc in Archive, navigate to the relevant directory, and click on the javadoc jar file:
Click Open, click OK, and you're done. As shown below, when you hover your cursor over an Apache Commons IO class name the javadoc appears:

Adding .class files to an Eclipse project

This is a total newbie question, but I can't figure it out so I figured that I would ask it here and see what happened.
Here's the problem: For my java programming class, we are supposed to download a .class file created by our instructors containing a custom-made class with methods that we are supposed to use in an assignment. I already know all of the code to create the class within my actual program, but I can't figure out which directory to place the .class file in. I have tried the src and bin folders inside the project directory, and even placed it directly into the project directory, but nothing seems to work. My instructors say to just put it in the same directory as my java program, but they are using jGRASP. What am I doing wrong?
You can place the class file anywhere, just be sure to add its directory to your project's build path. Right click on the project, then properties at the bottom then build path, til you find add class folder.
You have to create a project, or open your project if you have it already, then, while you are in the Java view, select File > New > File. It will open a window, you can create your file there and choose in which project folder do you want to put it. If in that window you press the button "Advanced >>" it will give you the option to "Link to file in the system", check it, and then you can browse your folders to the downloaded file.
I don't know if this solves your problem, it's what I understood from your question.
Your instructors class file must be in the build-time and the run-time class paths for your project to use it. Instructions assume that you are on a PC.
Run-time class path setup
Create a directory (perhaps C:\JavaRoot\classes). I will refer to this as the Classes directory.
If one does not already exist on your PC, create an environment variable named CLASSPATH
Add the Classes directory to your CLASSPATH envoronment variable
Download the class in question and put it in the Classes directory.
Build-time class path setup
Right mouse on your project in eclipse.
Select "Properties".
Select "Java Build Path".
Select the "Libraries" tab.
Click the "Add a Class Folder" button.
Browse to and select the Classes directory you created during "Run-time class path setup" above.
Click the "OK" button
For Eclipse use the following:
Project > Properties > Java Build Path > Libraries (Tab) > Add External Class Folder (Button)
If you have a configured project in Eclipse and you manually added a Java class file in some of the project directories, you just need to refresh the project for Eclipse to recognize it:
Right click in your project (in "Project Explorer" view) -> Refresh

How to add directory to classpath in an application run profile in IntelliJ IDEA?

I'm trying to add a directory to the classpath of an application run profile
If I override by using -cp x:target/classes in the VM settings, I get the following error:
java.lang.NoClassDefFoundError: com/intellij/rt/execution/application/AppMain
Any idea on how to add a directory to the classpath for my project?
In Intellij 13, it looks it's slightly different again. Here are the instructions for Intellij 13:
click on the Project view or unhide it by clicking on the "1: Project" button on the left border of the window or by pressing Alt + 1
find your project or sub-module and click on it to highlight it, then press F4, or right click and choose "Open Module Settings" (on IntelliJ 14 it became F12)
click on the dependencies tab
Click the "+" button on the right and select "Jars or directories..."
Find your path and click OK
In the dialog with "Choose Categories of Selected File", choose Classes (even if it's properties), press OK and OK again
You can now run your application and it will have the selected path in the class path
It appears that IntelliJ 11 has changed the method, and the checked answer no longer works for me. In case anyone else arrives here via a search engine, here's how I solved it in IntelliJ 11:
Go to the Project Structure, click on Modules, and click on your
Module
Choose the "Dependencies" tab
Click the "+" button on the right-hand side and select "Jars or directories..."
Add the directory(ies) you want (note you can multi-select) and click OK
In the dialog that comes up, select "classes" and NOT "jar directory"
Make sure you're using that Module in your run target
Note that step 5 seems to be the key difference. If you select "jar directory" it will look exactly the same in the IDE but won't include the path at runtime. There appears to be no way to determine whether you've previously selected "classes" or "jar directory" after the fact.
You can try -Xbootclasspath/a:path option of java application launcher. By description it specifies "a colon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path."
Set "VM options" like: "-cp $Classpath$;your_classpath"
I am using Idea 8.
in your module dependancies tab (in the project structure dialog). Add a "Module Library". There you can select a Jar Directory to add. Then make sure the run profile is using the Classpath and JDK of the correct module when it runs (this is in the run config dialog.
Simply check that the directory/package of the class is marked as "Sources Root". I believe the package should be application or execution in your case.
To do so, right click on the package, and select Mark Directory As->Sources Root.
You need not specify the classes folder. Intellij should be able to load it.
You will get this error if "Project Compiler output" is blank.
Just make sure that below value is set:
Project Settings -> Project -> Project Compiler output
to your projectDir/out folder
Open Settings
Under project Settings, select "project settings (1)"
On the left, select "modules"
Select the desired module
Select the "Sources" tab
In the tree structure, find the directory you want to add. Then either add as a source or test source.

Categories