How to Add a JAR to a Java Project in Eclipse

Table of contents:

How to Add a JAR to a Java Project in Eclipse
How to Add a JAR to a Java Project in Eclipse
Anonim

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

Add JARs to Project Build Paths in Eclipse (Java) Step 1
Add JARs to Project Build Paths in Eclipse (Java) Step 1

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 2
Add JARs to Project Build Paths in Eclipse (Java) Step 2

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

Add JARs to Project Build Paths in Eclipse (Java) Step 3
Add JARs to Project Build Paths in Eclipse (Java) Step 3

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

Add JARs to Project Build Paths in Eclipse (Java) Step 4
Add JARs to Project Build Paths in Eclipse (Java) Step 4

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 5
Add JARs to Project Build Paths in Eclipse (Java) Step 5

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 6
Add JARs to Project Build Paths in Eclipse (Java) Step 6

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 7
Add JARs to Project Build Paths in Eclipse (Java) Step 7

Step 4. Find the Build Path option

Place the mouse pointer on the "Build Path" menu item to access its submenu.

Add JARs to Project Build Paths in Eclipse (Java) Step 8
Add JARs to Project Build Paths in Eclipse (Java) Step 8

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)

Add JARs to Project Build Paths in Eclipse (Java) Step 9
Add JARs to Project Build Paths in Eclipse (Java) Step 9

Step 1. Click on the project name with the right mouse button

A context menu related to the project will be displayed.

Add JARs to Project Build Paths in Eclipse (Java) Step 10
Add JARs to Project Build Paths in Eclipse (Java) Step 10

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 11
Add JARs to Project Build Paths in Eclipse (Java) Step 11

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 12
Add JARs to Project Build Paths in Eclipse (Java) Step 12

Step 4. Select the Libraries tab

It is located at the top of the dialog that appears.

Add JARs to Project Build Paths in Eclipse (Java) Step 13
Add JARs to Project Build Paths in Eclipse (Java) Step 13

Step 5. Click the Add JARs button

It is located on the right of the project properties window.

Add JARs to Project Build Paths in Eclipse (Java) Step 14
Add JARs to Project Build Paths in Eclipse (Java) Step 14

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 15
Add JARs to Project Build Paths in Eclipse (Java) Step 15

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

Add JARs to Project Build Paths in Eclipse (Java) Step 16
Add JARs to Project Build Paths in Eclipse (Java) Step 16

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 17
Add JARs to Project Build Paths in Eclipse (Java) Step 17

Step 2. Place the mouse cursor on the Build Path entry

A new submenu will appear to the right of the first one.

Add JARs to Project Build Paths in Eclipse (Java) Step 18
Add JARs to Project Build Paths in Eclipse (Java) Step 18

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 19
Add JARs to Project Build Paths in Eclipse (Java) Step 19

Step 4. Click the Add Variable button

It is located on the right of the project properties window.

Add JARs to Project Build Paths in Eclipse (Java) Step 20
Add JARs to Project Build Paths in Eclipse (Java) Step 20

Step 5. Click the Configure Variables button

It is located at the bottom right of the "New Variables" dialog.

Add JARs to Project Build Paths in Eclipse (Java) Step 21
Add JARs to Project Build Paths in Eclipse (Java) Step 21

Step 6. Click on the New button

It is displayed at the bottom of the newly appeared dialog box.

Add JARs to Project Build Paths in Eclipse (Java) Step 22
Add JARs to Project Build Paths in Eclipse (Java) Step 22

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

Add JARs to Project Build Paths in Eclipse (Java) Step 23
Add JARs to Project Build Paths in Eclipse (Java) Step 23

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 24
Add JARs to Project Build Paths in Eclipse (Java) Step 24

Step 9. Click the OK button

In this way, the variable will be created based on the information provided.

Add JARs to Project Build Paths in Eclipse (Java) Step 25
Add JARs to Project Build Paths in Eclipse (Java) Step 25

Step 10. Click the OK button again

This will close the "Preferences" window.

Add JARs to Project Build Paths in Eclipse (Java) Step 26
Add JARs to Project Build Paths in Eclipse (Java) Step 26

Step 11. Select the variable from the list

Click on the corresponding name to select it.

Add JARs to Project Build Paths in Eclipse (Java) Step 27
Add JARs to Project Build Paths in Eclipse (Java) Step 27

Step 12. Click the Extend button

It is displayed in the lower right of the project variable list.

Add JARs to Project Build Paths in Eclipse (Java) Step 28
Add JARs to Project Build Paths in Eclipse (Java) Step 28

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 29
Add JARs to Project Build Paths in Eclipse (Java) Step 29

Step 14. Click the OK button

The dialog will close.

Add JARs to Project Build Paths in Eclipse (Java) Step 30
Add JARs to Project Build Paths in Eclipse (Java) Step 30

Step 15. Click the OK button to close the project's "classpath" variable dialog

Add JARs to Project Build Paths in Eclipse (Java) Step 31
Add JARs to Project Build Paths in Eclipse (Java) Step 31

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)

Add JARs to Project Build Paths in Eclipse (Java) Step 32
Add JARs to Project Build Paths in Eclipse (Java) Step 32

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 33
Add JARs to Project Build Paths in Eclipse (Java) Step 33

Step 2. Place the mouse cursor on the Build Path entry

A new submenu will appear to the right of the first one.

Add JARs to Project Build Paths in Eclipse (Java) Step 34
Add JARs to Project Build Paths in Eclipse (Java) Step 34

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 35
Add JARs to Project Build Paths in Eclipse (Java) Step 35

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)

Add JARs to Project Build Paths in Eclipse (Java) Step 36
Add JARs to Project Build Paths in Eclipse (Java) Step 36

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 37
Add JARs to Project Build Paths in Eclipse (Java) Step 37

Step 2. Place the mouse cursor on the Build Path entry

A new submenu will appear to the right of the first one.

Add JARs to Project Build Paths in Eclipse (Java) Step 38
Add JARs to Project Build Paths in Eclipse (Java) Step 38

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 39
Add JARs to Project Build Paths in Eclipse (Java) Step 39

Step 4. Select the Libraries tab

It is listed at the top of the project properties window.

Add JARs to Project Build Paths in Eclipse (Java) Step 40
Add JARs to Project Build Paths in Eclipse (Java) Step 40

Step 5. Click the Add External JARs button

It is located on the right of the project properties window.

Add JARs to Project Build Paths in Eclipse (Java) Step 41
Add JARs to Project Build Paths in Eclipse (Java) Step 41

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.

Add JARs to Project Build Paths in Eclipse (Java) Step 42
Add JARs to Project Build Paths in Eclipse (Java) Step 42

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.

Recommended: