Guava Sample Usage
Yes, using Guava is a great idea, especially for simple tasks as reading a file:
String contents = Files.toString(file, Charsets.UTF_8)
when compared to plain java versions like this:
BufferedReader in = null;
StringBuilder sb = new StringBuilder();
try {
in = new BufferedReader(new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
sb.append(str).append('\n');
}
} catch (IOException e) {
// do something here
} finally{
// and this should also be in a try / catch block
if(in!=null)in.close();
}
return sb.toString();
(But actually you need to have developed Java for years to really appreciate the easiness IMHO.)
Configuring Eclipse to use Guava
The easiest way to deal with such a library in Eclipse is to create a User library and add the user library to your project (right click project: Build Path -> Add Library -> User Library
).
Here is a short guide to generating a User Library:
- Open the menu entry
Preferences -> Java -> Build Path -> User Libraries
- Press
New...
, enter a name (e.g. 'Guava')
- Press
Add JARs...
and search for the JAR files you downloaded
(Unzip the Guava archive beforehand, inside you'll find the file guava-r07.jar
. Select that in this dialog, preferably after moving it to a more desirable location)
- Now select
Source Attachment -> Edit...
, and find the path to the file guava-src-r07
.
- If you want to, you can repeat this process for the JavaDoc location, pointing it to the
javadoc
folder from the guava distribution
- Press
OK
.
Now you should be all set, you can select the user library and add it to a project of your choice.
Using Maven
For completeness: being an advocate of dependency management with maven I would of course suggest to manage project dependencies with maven and m2eclipse instead of with user libraries.