Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add external library properly in Eclipse?

Tags:

eclipse

So today I downloaded Apache Commons Lang library (binary, zip format). I extracted it to C:\eclipse\commons-lang-2.5 folder. There are a commons-lang-2.5.jar, a commons-lang-2.5-javadoc.jar, and a commons-lang-2.5-sources.jar inside, and a folder for HTML Javadoc. I started Eclipse, added commons-lang-2.5.jar, and set its source and Javadoc respectively as the screenshot below. (http://img43.imageshack.us/img43/9378/eclipsev.png)

Eclipse screenshot

My question is, is there a convenient or standard way to add external libraries? Or am I actually doing the right thing?

like image 596
zihaoyu Avatar asked May 13 '10 05:05

zihaoyu


People also ask

Why can I not add external JARs in Eclipse?

If your project builds with Java 9+ make sure you've selected Classpath (as shown here). You should also be able to add the JAR with right-click on the project > Build Path > Add External Archives....

How do I import an external library?

Click on Project files. Copy the jar file to the libs folder. Here, you can see our added jar file under the libs folder. Right-click on the jar file and select Add As Library.


3 Answers

Recommendation:

  1. Create a "lib" folder and keep all your jars in the folder.
  2. Subsequently, add all the jar files in the lib folder into your build path by using Project => Properties => Java Build Path => Libraries => Add JAR ...

btw, there' no screenshot. Can you give the link for the screen shot so that I may be able to help our better...

like image 95
gvaish Avatar answered Oct 25 '22 21:10

gvaish


You must add jar file on lib folder and then right click on jar file and click "build path"-->add to build path and now you can write that jar file code

like image 40
parsa Avatar answered Oct 25 '22 22:10

parsa


Use maven

You don't have to download all jar's into a folder by yourself! - use maven. It's based on a public repository, and you manage your dependencies in an xml file.

Your project will have a pom.xml file that going to look like this:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

and in this file you manage the external library dependencies

for instance, if you wish to add this dependency - http://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4

you will need to modify your pom.xml like so:

<project>
  ...
  <dependencies>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>

  </dependencies>
  ...
</project>

and save it. that's it

  • Read this getting started tutorial - https://maven.apache.org/guides/getting-started/
  • eclipse plugin - https://maven.apache.org/plugins/maven-eclipse-plugin
like image 32
Jossef Harush Kadouri Avatar answered Oct 25 '22 22:10

Jossef Harush Kadouri