How to add JAR file in Eclipse Project Build path - External dependency

In this Java Eclipse tutorial, I will show you two ways to add external JAR files in Eclipse Java projects. Many times we need to use external JAR files in our Java application for different needs e.g. for general purpose you may use Google Guava or Apache Commons. If you are using Spring or Hibernate framework then you need their JAR files as well. In order to use any third-party library or framework, we have to add their JAR files in the classpath, to compile and run our Java programs. Since Eclipse is the most popular IDE for developing Java applications, it's important to know how you can add external JARs into your Java project's build path. For example, suppose, you have downloaded Apache commons JAR file from the internet and wants to add that file to your Java project. In order to use classes and methods from that library, your project must reference those and this is what we will learn here.

You can even attach Java source files, corresponding to a JAR file for seeing actual code while navigating and debugging your Java program. This is really useful when you are stepping into the source code of third-party library or any internal library while debugging. In my last article, I have shown you how to attach source to a JAR file, which ideally should be next to this article, but never mind you can refer them now.

Though you can add any external JAR files in your Java project on Eclipse, I suggest using Maven as a build tool for dependency management, it's a lot easier to manage a central local repository using Maven for keeping those useful JAR files, instead of searching on the internet, downloading it and then manually adding into your Java project's build path.

2 ways to add JAR file in Java project build path in Eclipse

Here are a couple of ways to add .jar files in Java project on Eclipse IDE. Though there are different ways you can reuse JAR files e.g. in form of library, Eclipse provides it's own bundled JRE and JUnit library, but most of the time, we add .jar files as an external dependency. These are the JAR files, which you might have downloaded from the internet or copied it from any other project.

1) Open project properties windows by following steps, Select Project -> Right Click -> Properties, then select Java Build Path and go to Libraries tab and click on Add External JARs. This allows you to reference any JAR file in any folder on your machine. Just make sure that location of the JAR file is accessible. Once you have selected the JAR file after browsing, it will appear inside your project as Referenced Libraries.

How to add JAR file in Eclipse Project Build path

2) This is the shortcut of earlier method, In this method, you don't need to navigate project properties windows. Just Select project -> Right Click -> Build Path -> Add external archives. This will open the same file explorer like earlier method, which allows you to browse through directories and choose external JAR. Once successfully added, it will appear inside Referenced Libraries of your project.

That's all on how to add JAR files in Eclipse project build path. The second way is much quicker than first way, as you only need a couple of selection, but knowledge of project properties windows helps a lot, e.g. while changing compiler settings for your project as seen in my post avoiding must override method error in Eclipse. You can use anyway as per your liking, but make sure that JAR appears in referenced libraries inside your project when you see it from project explorer.

Other Eclipse tutorials for Java Programmers you may like
  • Top 30 Eclipse keyboard shortcuts for Java programmers (shortcuts)
  • 3 Books to learn Eclipse IDE for Java Programmers (books)
  • 10 Essential Eclipse debugging tips for Java developer (tips)
  • How to do remote debugging in Eclipse (guide)
  • Eclipse shortcut to comment and uncomment several lines (article)
  • How to increase heap size of Java Program in Eclipse (tutorial)
  • How to solve the "main class not found error" in Eclipse? (guide)
  • How to connect Eclipse to Oracle database? (guide)

Thanks for reading this article, if you really like the content then please share with your friends and colleagues. If you have any comments or suggestions then please drop a comment. If you want to learn more about Eclipse IDE, I suggest reading Eclipse IDE book from Lars Vogel.

Further Learning
Beginners Eclipse Java IDE Training Course
Eclipse Debugging Techniques And Tricks
The Eclipse Guided Tour - Part 1 and 2

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.