2011-07-09

Adding libraries to Eclipse

Being new to Eclipse, I could not figure out how to add libraries to my project. By libraries, I meant already-compiled jars, such as JodaTime, SLF4J, Postgres JDBC driver, etc. And I want to provide Eclipse with access to those utilities’ source code and JavaDoc if available.

I struggled until I read this article.

Other IDEs have you define the executable jar, its source code jar or folder, and its JavaDoc jar or folder simultaneously as all equal parts of one named library. The eclipse way is different. Instead of 3 parts to one library, Eclipse establishes a hierarchy of the library (just a name), one or more executable jars, and then attaching the source code and JavaDoc as attributes on each executable jar.

So, the steps to establish a library:
  1. Establish the name of the library.
    On a Mac, that would be Eclipse > Preferences > Java > Build Path > User Libraries > New.
  2. Add a jar.
    This would be the executable jar of the utility.
    For example, with JodaTime: joda-time-1.6.2.jar
  3. Notice the 4 sub-items that appear below the added jar:
    - Source attachment:
    - Javadoc location:
    - Native library location:
    - Access rules:
  4. Select ‘Source attachment:’ and click Edit.
    For example, with JodaTime: joda-time-1.6.2-sources.jar
  5. Do similarly if you have Javadoc available.
    For example, with JodaTime: joda-time-1.6.2-javadoc.jar
  6. Click OK to close the prefs.
Now to use those established libraries in your project:
  1. Context-click your project in the Package Explorer.
  2. Choose Build Path > Add Libraries
  3. Choose ‘User Library’ and click ‘Next’.
  4. Check the libraries that you want to add.
    The libraries appear as items inside your project in the Package Explorer.