Wednesday, March 20, 2024

How to add JAR files in Eclipse Project's Build path? Example

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 like for general purposes 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 an Apache commons JAR file from the internet and want 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 a third-party library or any internal library while debugging. 

In my earlier article, I have shown you how to attach sources to a JAR file, which ideally should be next to this article, but never mind you can refer to 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 the Java project on Eclipse IDE. Though there are different ways you can reuse JAR files e.g. in form of a library, Eclipse provides its 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 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 the 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 the 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 as the 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.

How to add JAR files into Eclipse projects



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

You can also use this tip to fix errors and exception like ClassNotFoundException and NoClassDefFoundError in Java, but I highly recommend you to use a tool like Maven or Gradle to manage the dependencies instead of adding JAR files manually. This is easy and also the standard way to manage dependencies in Java. 

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 developers (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 the Oracle database? (guide)

Thanks for reading this article, if you really like the content then please share it 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 joining these best Eclipse IDE courses for Java Programmers and Developers. 

No comments:

Post a Comment

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