Saturday 31 December 2011

How to Adding a library (.jar ) to your project in Eclipse for Selenium automation


10. Using jars (libraries)

10.1. Adding a library (.jar ) to your project

The following describes how to add Java libraries to your project. Java libraries are distributed via "jar" files. It assumes that you have a jar file available; if not feel free to skip this step.
Create a new Java project de.vogella.eclipse.ide.jars. Then, create a new folder called lib, by right clicking on your project and selecting New → Folder.

Creating a new folder

From the menu select File → Import → General → File System. Select your jar and select the folder lib as target. Alternatively, just copy and paste your jar file into the "lib" folder.
Right click on your project and select Properties. Under Java Build Path → Libraries select the button "Add JARs".
The following example shows how the result would look like, if the junit-4.4.jar had been added to the project.

Adding a jar to the current project

Afterwards you can use the classes contained in the jar file in your Java source code.

10.2. Show source code for jar

As said earlier you can open any class via positioning the cursor on the class in an editor and pressing F3. Alternatively, you can press CTRL+Shift+T. This will show a dialog in which you can enter the class name to open it.
If the source code is not available when you attempt to open a class, the editor will show the decompiled byte-code of that class.
This happens if you open a class from a jar file and the source for this jar is not available.
To browse the source of a type contained in a library (i.e. .jar file), you can attach a source archive or source folder to that library. Afterwards the editor will show the source instead of the byte-code.
In addition setting the source attachment allows debugging this source code.
The Source Attachment dialog can be reached in the Java Build Path page of a project. To open this page, right click on a project → Properties → Java Build Path. On the "Libraries" tab, expand the library's node, select the "Source attachment" attribute and press Edit.
In the Location path field, enter the path of an archive or a folder containing the source.
The following shows this for the standard Java library. If you have the Java Development Kit (JDK) installed, you should find the source in the JDK installation folder. The file is typically called "src.zip".

Maintaining the location of the source attachment to an jar

10.3. Add the Javadoc for a jar

It is also possible to add Javadoc to a library which you use.
Download the Javadoc of the jar and put it somewhere in your filesystem.
Open the Java Build Path page of a project via Right click on a project → Properties → Java Build Path. On the "Libraries" tab expand the library's node, select the "Javadoc location" attribute and press Edit.
Enter the location to the file which contains the Javadoc.

Maintain the location to the Javadoc file for a jar file