Resources‎ > ‎Java Basics‎ > ‎

Third-Party Libraries

There are many places you can download Java libraries that provide APIs for performing common tasks.  Google, for example, provides many libraries that provide access to their services.  In order to access a user's Google calendar, you can simply download a Java library and invoke the appropriate methods.

For this example, we will consider the Sun JavaMail library.  This library makes it very easy to send email from a Java program.

Downloading

The first step toward using a library is to download it.  To download the JavaMail library, visit http://java.sun.com/products/javamail/downloads/index.html and follow the steps to download the zip file.  The unzipped folder typically contains the following subdirectories:
  1. lib - The lib directory contains one or more jar files.  A jar, or java archive, is simply a collection of class files bundled together.  It is akin to a zip file for java files.  The jar files are what you will include in your classpath in order to use the libraries.
  2. src - You may find a src directory that contains the java source files for the library.  If included, you can rebuild the library if you wish. You can also gain a better understanding of how the library works, particularly if the documentation is sparse.
  3. doc - The doc directory will contain documentation, and may contain javadoc for the library.
  4. samples/demos - You may find a directory containing samples or demos that use the library.
  5. README - The README file typically contains all necessary information about installing/running/using the library and the elements contained in the distribution. If the library relies upon another library, this information will typically be found in the README. 

Setting the Classpath

The classpath tells your development environment where to find any thrid-party or user-defined libraries.  In order to compile code that uses a particular library, the development environment must know where to find the class files associated with that library.  Similarly, to run code that uses a third-party library the JRE must know where to find the class files. 

If you have a directory containing class files, you will want to add the directory to your classpath.  If your class files are in a jar file, you can add the jar file to your classpath.  From the command line, you can use the -cp option as follows:

    java -cp /Users/srollins/tools/javamail-1.4.1/mail.jar MyProgram

Notice that multiple jars/directories can be put into a colon-separated list.

If using Eclipse, you can set the classpath as follows:
  1. Right click the project and select Properties.
  2. Select Java Build Path on the left.
  3. Select Libraries.
  4. Select Add External Jars.
  5. Navigate to the jar and click Open.

Running Sample/Demo Applications

A good place to start is by running any samples provided and looking at the code.  For the JavaMail example, the msgsend application demonstrates how to send a message.  The following sends a message using the SMTP server smtp.usfca.edu:

        java -cp /Users/srollins/tools/javamail-1.4.1/mail.jar:. msgsend -M smtp.usfca.edu

Comments