When a program created in Java needs to use JAR libraries (from the English "Java ARchive") to work, the project must be configured to correctly include all the libraries it needs during compilation. Fortunately, the Eclipse editor makes this process extremely simple and easy to put into practice. The version of the program used in this article is the following: Eclipse Java - Ganymede 3.4.0.
Steps
Part 1 of 6: Adding an Internal JAR File
Step 1. Create a new folder called lib within the project
The acronym refers to the word "libraries", ie libraries, and is the point where all the JAR files that will be recalled within the project code will be stored.
Step 2. Copy and paste the Jar files you need into the lib folder
Select all the JAR archives, then click on one of the files with the right mouse button. At this point, click on "Copy" in the menu that appeared, then paste the files into the "lib" folder by accessing the menu File and choosing the option Paste. Alternatively, you can use the key combination "Ctrl + V" or "Command + V".
Step 3. Update the project references
Click on the project name with the right mouse button and choose the Refresh option from the menu that will appear. Folder lib it will be visible within the Eclipse interface and will contain all the JAR files you have selected.
Part 2 of 6: Configuring the Build Path
Step 1. Expand the lib folder visible within Eclipse's "Package Explorer" panel
Click on the small arrow icon, located to the left of the "lib" folder, to view the list of files it contains.
Step 2. Select all the JAR files you need
Hold down the Ctrl key, then click on the JAR files you want to include in the selection.
Step 3. Click on the selected JAR files with the right mouse button
A context menu will be displayed to the right of the files in question.
Step 4. Find the Build Path option
Place the mouse pointer on the "Build Path" menu item to access its submenu.
Step 5. Select the Add to Build Path option
All indicated JAR files will be removed from the folder lib Eclipse and transferred to the card Referenced Libraries.
Part 3 of 6: Configuring the Build Path (Alternative Method)
Step 1. Click on the project name with the right mouse button
A context menu related to the project will be displayed.
Step 2. Find the Build Path entry
It is one of the options listed in the context menu that appeared when you right-clicked on the project name. A new submenu will be shown to the right of the first one.
Step 3. Click on the Configure Build Path entry
The project properties window will be displayed in which it will be possible to configure the paths where to find all the files necessary for the build of the project.
Step 4. Select the Libraries tab
It is located at the top of the dialog that appears.
Step 5. Click the Add JARs button
It is located on the right of the project properties window.
Step 6. Select the JAR files you want to include in the project and click the OK button
The JAR files will appear in the list of libraries that will be used to build the project.
Step 7. Click on the OK button to close the project properties window
The selected JAR files will be visible in the section Referenced Libraries, instead of in the folder lib of the project.
Part 4 of 6: Adding an External JAR File
Step 1. Click on the project name with the right mouse button
A context menu related to the project will be displayed.
-
Note:
It is always better to use JAR files that are present within the project or another project so that you can keep under control all the dependencies of your program directly from Eclipse.
Step 2. Place the mouse cursor on the Build Path entry
A new submenu will appear to the right of the first one.
Step 3. Click on the Configure Build Path entry
The project properties window will be displayed in which it will be possible to configure the paths where to find all the files necessary for the build of the project.
Step 4. Click the Add Variable button
It is located on the right of the project properties window.
Step 5. Click the Configure Variables button
It is located at the bottom right of the "New Variables" dialog.
Step 6. Click on the New button
It is displayed at the bottom of the newly appeared dialog box.
Step 7. Name the new variable you are creating
For example, if the JAR files refer to the Tomcat web server, it might be useful to choose to use the name "TOMCAT_JAR".
Step 8. Navigate to the directory that contains the JAR file
Click on the button Folder, then select the folder where the JAR file to use in the project is stored.
Alternatively, you can click on the button File and select a single JAR file to assign to the variable.
Step 9. Click the OK button
In this way, the variable will be created based on the information provided.
Step 10. Click the OK button again
This will close the "Preferences" window.
Step 11. Select the variable from the list
Click on the corresponding name to select it.
Step 12. Click the Extend button
It is displayed in the lower right of the project variable list.
Step 13. Select the JAR files you want to insert into the project
Click on the file name. Hold down the ⇧ Shift or Ctrl key if you want to select multiple JAR files.
Step 14. Click the OK button
The dialog will close.
Step 15. Click the OK button to close the project's "classpath" variable dialog
Step 16. Click the OK button
The dialog box related to the configuration of the "build path" of the project will be closed.
If you are working on a project that is shared with other people, they too will have to define the same variable you just created. The users you share the project with will need to access the menu Window Eclipse, choose the item Preferences, click on the tab Java, select the item Build Path and finally click on the tab Classpath Variables.
Part 5 of 6: Adding an External JAR File (Alternative First Method)
Step 1. Click on the project name with the right mouse button
A context menu related to the project will be displayed.
-
Note:
using this method, the external JAR file will need to be placed in the same place on the hard drive of all the users' computers that will use this project. For this reason, sharing this project with other people could be more complicated.
Step 2. Place the mouse cursor on the Build Path entry
A new submenu will appear to the right of the first one.
Step 3. Click on the Add External Archives option
It is one of the submenu items that appeared when you chose the "Build Path" option.
Step 4. Select the JAR file to use and click the Open button
All selected JAR archives will be added to the project and will be listed in the section Referenced Libraries.
Part 6 of 6: Adding an External JAR File (Second Alternative Method)
Step 1. Click on the project name with the right mouse button
A context menu related to the project will be displayed.
-
Note:
using this method, the external JAR file will need to be placed in the same place on the hard drive of all the users' computers that will use this project. For this reason, sharing this project with other people could be more complicated.
Step 2. Place the mouse cursor on the Build Path entry
A new submenu will appear to the right of the first one.
Step 3. Click on the Configure Build Path entry
The project properties window will be displayed in which it will be possible to configure the paths where to find all the files necessary for the build of the project.
Step 4. Select the Libraries tab
It is listed at the top of the project properties window.
Step 5. Click the Add External JARs button
It is located on the right of the project properties window.
Step 6. Select the JAR file to use and click the Open button
All the selected JAR archives will appear in the list of libraries related to the "Build Path" of the project.
Step 7. Click the OK button to close the project properties window
At this point, all the JAR files you added to the project will be listed in the section Referenced Libraries.
Advice
- When adding a new file or folder to an Eclipse project without using the editor directly, all affected projects must be refreshed so that Eclipse can detect the new content. Otherwise, errors will be generated when compiling and creating the project's executable file.
- Even if a project's internal JAR file disappears from the folder lib, it will still be present in the computer's file system. This is simply Eclipse's way of letting the user know that the indicated files have actually been added to the project.
-
To be precise, you could create a folder in which to store the documentation related to the project. Follow these instructions:
- Click on the JAR file displayed in the "Reference Libraries" tab of the "Package Explorer" panel;
- Select the "Javadoc" tab and enter the folder (or URL) where the project documentation is stored (attention: Eclipse will generate an error when validating this change, but don't worry because everything will work anyway);
- Select the "Java Source Attachment" entry, then find the folder or JAR file that contains the source files.