Add JAR files to a Build Path in Eclipse (with images)

Table of contents:

Add JAR files to a Build Path in Eclipse (with images)
Add JAR files to a Build Path in Eclipse (with images)
Anonim

If your Java project requires JAR (Java Archive) libraries to function, you may need to configure your project to include the libraries in the file path. Fortunately, it's easy to do with Eclipse, and easy to remember. The version used here is Eclipse Java - Ganymede 3.4.0.

Steps

Part 1 of 6: Adding internal JAR files

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

Step 1. Create a new folder called lib in your project folder

This stands for 'libraries' and contains the JAR files that you will use for that project.

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

Step 2. Copy and paste the JAR files you need to lib

Select the JAR files you need and right click on them. Select them and click copy. Then paste them into the lib folder by pressing File clicking and then To stick or using the Control or Command-V keys.

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

Step 3. Refresh your project

You do this by right-clicking on the name of the project and clicking Refresh. The map lib is now visible in Eclipse with the JAR files in it.

Part 2 of 6: Configuring your Build Path

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

Step 1. Open the lib folder in Eclipse

Click the arrow to the left of the folder to open it.

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

Step 2. Select all the JAR files you need

Hold down ⇧ Shift and click the JAR files in the open folder.

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

Step 3. Right click on the JAR files

A pop-up menu will now open on the right.

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

Step 4. Go to Build Path

Hovering over 'Build Path' will bring up a submenu on the left.

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

Step 5. Select Add to Build Path

The JAR files disappear from the lib and reappear in Referenced Libraries.

Part 3 of 6: Configuring the Build Path (alternate method)

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

Step 1. Right click on the project name

This will show a pop-up menu on the right.

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

Step 2. Go to Build Path

This can be found in the pop-up menu that appears when you right-click on the project name. A submenu will then be shown to the right of it.

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

Step #3. Click Configure Build Path

The project properties window opens showing the Build Path configurations.

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

Step #4. Select the Libraries tab

This can be found at the top of the project properties window.

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

Step 5. Click Add JAR Files

This can be found on the right side of the project properties window.

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

Step 6. Select the JAR files you need and click OK

The JAR files now appear in the list of libraries in Build Path.

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

Step 7. Click OK to close the properties window

The JAR files are now in Referenced Libraries instead of in lib.

Part 4 of 6: Adding external JAR files

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

Step 1. Right click on the project name

A pop-up menu will appear on the right.

  • Remark:

    It's much better to reference JAR files that exist in your own project or in other projects - this gives you the ability to check all dependencies on the version control system.

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

Step 2. Go to Build Path

A submenu is displayed on the right side.

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

Step #3. Click Configure Build Path

The project properties window appears with the Build Path configurations.

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

Step #4. Click Add Variable

You'll see this option to the right of the project's properties window.

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

Step 5. Click Configure Variables

You will see this option at the bottom of the New Variables window.

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

Step 6. Click New

You can find this at the bottom of the preferences window.

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

Step 7. Type a name for the new variable

For example, when it comes to JAR files for Tomcat, you might call it 'TOMCAT_JARS'.

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

Step 8. Browse to the directory containing the JAR for the path

Click on the folderbutton and browse to the directory containing the JAR path.

  • You can also click File click and choose a specific JAR file for the variable, if you want.
Add JARs to Project Build Paths in Eclipse (Java) Step 24

Step 9. Click OK

This defines the variables.

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

Step 10. Click OK

This closes the preferences window.

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

Step 11. Select the variable from the list

Click on the variable to select it.

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

Step 12. Click Expand

This is the button to the right of the variable list.

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

Step 13. Select the JAR files you want to add to the classpath

Click it to select the JAR files. Hold ⇧ Shift to select multiple JAR files.

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

Step 14. Click OK

This closes the extended dialog box.

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

Step 15. Click OK

This closes the dialog for the new classpath variable.

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

Step 16. Click OK

This closes the Build Path settings window.

  • If you share the project with someone else, they must also define the variable. They can define it under Window >Preferences->Java->Build Path->Classpath Variables.

    Part 5 of 6: Adding external JAR files (Alternative Method 1)

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

    Step 1. Right click on the project name

    This will show a pop-up menu on the side.

    Using this method, the external JAR should be in the same location on the hard drive for everyone using this project. This makes sharing a common project easier

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

    Step 2. Go to Build Path

    A submenu is shown on the right.

    Click Add External Archives. This is a submenu of Build Path.

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

    Step 1. Select the JAR files you need and click Open

    The JAR files now appear in Referenced Libraries.

    Part 6 of 6: Adding external JAR files (Alternative Method 2)

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

    Step 1. Right click on the project name

    A pop-up menu will now appear on the right side.

    • Remark:

      Using this method, the external JAR should be in the same location on the hard drive for everyone using this project. This will make sharing a common project easier.

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

    Step 2. Go to Build Path

    This is in the pop-up menu that appears when you right-click on the project name.

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

    Step #3. Click Configure Build Path

    The project properties window will appear containing your Build Path configurations.

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

    Step #4. Select the Libraries tab

    It's at the top of the project's properties window.

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

    Step 5. Click Add External JAR Files

    It's on the right side of the project's properties window.

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

    Step 6. Select the JAR files you need and click Open

    The JAR files now appear in the list of libraries in the Build Path.

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

    Step 7. Click OK to close the properties window

    The JAR files are now in Referenced Libraries.

    Tips

    • When you add new files or folders to your projects in Eclipse through anything other than Eclipse, you must refresh the affected projects to let Eclipse know that the new files are there, otherwise you may encounter compiler or Build Path errors.
    • Although the internal JAR files disappear from lib, they are still present in the file system. It's just Eclipse's perspective that these JAR files were added.
    • Just to be safe, you could create a folder to document your code. You do this as follows:

      • Right click on the.JAR in Reference Libraries in the package explorer.
      • Select the Javadoc tab and specify the folder (or URL) where the documentation is located. (Note: Eclipse will not like this and validation will fail. But don't worry, it will still work.)
      • Select Java Source Attachment and locate the folder or.JAR file containing the sources.

Popular by topic