How to Add JAR to Project Build Path on Eclipse (Java)

Table of contents:

How to Add JAR to Project Build Path on Eclipse (Java)
How to Add JAR to Project Build Path on Eclipse (Java)

Video: How to Add JAR to Project Build Path on Eclipse (Java)

Video: How to Add JAR to Project Build Path on Eclipse (Java)
Video: 3 EASY Ways to Find and Remove Duplicates in Excel 2024, December
Anonim

When your Java project needs a JAR (Java Archive) library to function, you need to configure it to include the library in its build path. Thanks to Eclipse, this process is simple and easy to remember. This article covers Java Eclipse - Ganymede 3.4.0.

Step

Method 1 of 2: Adding an Internal JAR

Step 1. Copy the JAR that will be used for your project

Here's how to do it:

  • Create a folder called lib in your project folder. “Lib” means library and holds all the JARs that will be used for this project.

    Add JARs to Project Build Paths in Eclipse (Java) Step 1Bullet1
    Add JARs to Project Build Paths in Eclipse (Java) Step 1Bullet1
  • Copy the required JAR to the lib.

    Add JARs to Project Build Paths in Eclipse (Java) Step 1Bullet2
    Add JARs to Project Build Paths in Eclipse (Java) Step 1Bullet2
  • Reload your project by right-clicking the project name and selecting Refresh. Folders lib will now be viewable in eclipse with all JARs in it.

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

Step 2. Complete one of the methods below to configure your build path

Method 1

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

Step 1. Develop lib in eclipse and select all required JARs

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

Step 2. Right click the JAR and go to Build Path

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

Step 3. Select Add to Build Path

JAR will disappear from lib and reappear in Referenced Libraries.

Method 2

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

Step 1. Right click the project name and go to Build Path

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

Step 2. Select Configure Build Path

.. and the project properties window will appear showing your build path configuration.

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

Step 3. Select the Libraries label

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

Step 4. Click Add JARs

..

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

Step 5. Locate and select the desired JAR and click OK

The JAR will now appear in the list in the build path.

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

Step 6. Click OK to close the properties window

JAR will now be in Referenced Libraries instead lib.

Method 2 of 2: Adding External JAR

Note: It's better that you reference the existing JAR in your project or another project. This allows you to check in all dependencies to your version control system (you must use version control).

Use one of the following methods.

Method 1

This is the recommended method as it allows different developers of similar projects to find their external JARs in different places.

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

Step 1. Right click the project name and go to Build Path

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

Step 2. Select Configure Build Path

.. and the project properties window will appear showing your build path configuration.

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

Step 3. Click Add Variable

..

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

Step 4. Click Configure Variables

..

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

Step 5. Click New

..

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

Step 6. Type a name for the new variable

For example, if all of these JARs are for Tomcat, we recommend typing TOMCAT_JAR.

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

Step 7. Go to the directory that holds the JAR for the path (you can also select a specific JAR file for the variable)

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

Step 8. Click Ok to define the variables

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

Step 9. Click Ok to close the preferences dialog

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

Step 10. Select a variable from the list

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

Step 11. Click Extend

..

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

Step 12. Select the JAR you want to add to the classpath

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

Step 13. Click Ok to close the dialog

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

Step 14. Click Ok to close the new classpath variables dialog

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

Step 15. Click Ok to close the build path setup dialog

  • If you share this project with other people, they must also define the variables. They can determine it through

    ''''Window->Preferences->Java->Build Path->Classpath Variables''''

Method 2

Note that if you use this method, the external JAR must be in the same location on the hard drive as anyone using this project. This makes sharing projects more difficult.

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

Step 1. Right click the project name and go to Build Path

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

Step 2. Select Add External Archives

..

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

Step 3. Locate and select the desired JAR and click Open

JAR will appear in Referenced Libraries.

Method 3

Note that if you use this method, the external JAR must be in the same location on the hard drive as anyone using this project. This makes sharing projects more difficult.

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

Step 1. Right click the project name and go to Build Path

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

Step 2. Select Configure Build Path

.. and the project properties window will appear in your build path configuration.

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

Step 3. Select the Libraries label

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

Step 4. Click on Add External JARs

..

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

Step 5. Find and select the desired JAR and click Open

The JAR will now appear in the list of libraries in the build path.

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

Step 6. Click OK to close the properties window

JAR will now be inside Referenced Libraries.

Tips

  • Whenever you add new files or folders to a project in Eclipse via anything other than Eclipse, you must reload (refresh) the associated project to notify Eclipse that the new files are there. Otherwise, you will encounter compiler or Build path errors.
  • Even though the internal JAR disappeared from lib, the files are still in the file system. This is just Eclipse's way of telling you that the JAR files have been added.
  • Just to be safe, we recommend that you create a folder to document your code. Here's how:

    • Right-click the. JAR in the Reference Libraries in the package explorer.
    • Select the label Javadoc and type it in the folder(or URL) where your documentation is located. (Note: Eclipse won't like this and your validation will fail. Don't worry though, it'll still work).
    • Select Java Source Attachment and find the folder or. JAR file that has your sources.

Recommended: