If you are using Maven or Gradle or something of the sort, there is a much easier way to import Guava into your project. This tutorial is just for those who want to use Guava but are not using a framework to handle dependencies.
Step 1 - Download the JAR files:
- Go to the Guava Release History Page and find the latest release (Release 14 at the time of this writing)
- Click on the latest release in the 'Release' column. This will take you to a wiki about that specific release
- Under the 'Using Guava in Your Project' section there are 4 jars that you can download. Download the Javadocs, the source, and the main jar.
The GWT jar is not needed. |
Step 2 - Copy Jar files into lib folder
- If your project doesn't have a lib folder you can add one by right clicking the project and going to New > Folder.
- Copy the 3 jar files you downloaded into the lib folder
Step 3 - Navigate to the Java Build Path Properties
- Inside the Package Explorer View, right click the project you wish to add Guava to.
- Click on Properties.
- Click 'Java Build Path' from the list on the left.
- Click on the 'Libraries' tab
Step 4 - Add JAR files to the project.
- Click 'Add JARs...'. This will prompt you to search your Project Tree for a JAR file.
- Navigate to the lib folder. Click on the guava-xx.x.jar (not the source or javadoc jars, we'll use them in just a second)
- The guava jar should now show up in the list of libraries. Go ahead and expand the guava-xx.x.jar in the list.
- Double click 'Source attachment'
- A dialog will appear prompting you to point eclipse to the source jar.
- Click Browse.
- Navigate to the lib folder of your project and select guava-xx.x-sources.jar, then hit OK, and hit okay on the dialog, this will bring you back to the Libraries Dialog.
- Back on the Properties Dialog, double click on 'Javadoc Location'
- This will bring up a dialog to find the javadoc jar file.
- Click on the 'Javadoc in archive' radio button
- Click on the 'Workspace file' radio button
- Click Browse
- This will bring up your project tree again.
- Go to your lib folder and select the guava-xx.x-javadoc.jar file
- Click OK until you are back on the regular workspace screen.
- In your Package Explorer there should now be a 'Referenced Libraries' section with the guava jar listed under it
Now you can use the library. Code completion should show the javadocs and debugging should allow you to step into the guava source (if for some reason you need to).
Here I use the ImmutableList class from the Guava libraries. |
No comments:
Post a Comment