Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The very basics for using Guava [closed]

Tags:

java

guava

I totally did google this. But everything I found is about the next step past where I am.

I am returning to programming after a long time away, and learning Java (& OO) for the first time. I asked about reading a file, and someone suggested I not try to learn Java's contortions for this, but instead use Guava. After googling, I found out roughly what Guava is and does (sweet!) and I downloaded it.

Then I discovered that I don't actually know what "use Guava" means.

I have used imports (like junit, plus basic classes like, I don't know, Math and stuff), and I expect it will be similar. But I don't know, for example, what folder to put Guava in, nor whether Eclipse will recognize it. (Maybe that's the only thing I need to know? I don't know that either.)

What I am asking for here is this: How do I go from awareness of a new tech (namely Guava libraries) to having code written using that tech, in Eclipse, and executing?

like image 366
onealexharms Avatar asked Nov 23 '10 14:11

onealexharms


2 Answers

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:

  1. Open the menu entry Preferences -> Java -> Build Path -> User Libraries
  2. Press New..., enter a name (e.g. 'Guava')
  3. 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)
  4. Now select Source Attachment -> Edit..., and find the path to the file guava-src-r07.
  5. If you want to, you can repeat this process for the JavaDoc location, pointing it to the javadoc folder from the guava distribution
  6. 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.

like image 165
Sean Patrick Floyd Avatar answered Oct 06 '22 01:10

Sean Patrick Floyd


As Sean mentioned, I would highly recommend that you use Maven to import the libraries. It makes it simple to upgrade dependencies later and, once you install m2eclipse, Eclipse will pick the Guava libraries up for you. All you need in your Maven pom.xml to "use Guava" is something along the lines of the following in your dependencies section.

 <dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>15.0</version>
 </dependency>
like image 26
Niraj Tolia Avatar answered Oct 06 '22 01:10

Niraj Tolia