18

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
onealexharms
  • 197
  • 1
  • 1
  • 6
  • Are you writing a simple java project or a web project in java? – Buhake Sindi Nov 23 '10 at 14:33
  • [Examples](http://codemunchies.com/tag/google-collections/) in Guava. – Emil Nov 23 '10 at 15:04
  • I'm only writing toy projects right now, running on my own computer. – onealexharms Nov 23 '10 at 15:49
  • You should start with the absolute basics - learn java and how it builds before you 'learn' guava. Else you will never a) understand what eclipse does for you and b) why the way you do it with guava is better than doing it by hand every time. – atamanroman Oct 24 '13 at 08:12

2 Answers2

36

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.

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Thanks for the answer part of your comment (starting with "One way to deal with..."). – onealexharms Nov 23 '10 at 23:43
  • This question is not answered. After [Window ->]Preferences -> Java -> Build Path -> User Libraries, there are two options: New and Import. Import is looking for something like .userlibraries, which the Guava download doesn't seem to include. If I choose "New" it lets me enter the name of my "new library" but I don't know how to make that actually *be* a library. The help file doesn't tell me either. – onealexharms Nov 24 '10 at 14:13
  • @Angela I have now explained it in more detail. – Sean Patrick Floyd Nov 24 '10 at 14:54
  • `Charset.UTF_8` does not seem to be a valid way to get to a Charset object. Are you sure you didn't mean `StandardCharsets.UTF_8`? (Considering the age of the post, this may have been a language change. Accessing Charset objects through StandardCharsets should be effective - using Java 8.) – romnempire Dec 15 '14 at 21:16
  • @romnempire It was actually `Charsets.UTF_8`, sorry for the typo. And that class existed in Guava long before Java 7 introduced the `StandardCharsets`class – Sean Patrick Floyd Dec 16 '14 at 08:03
5

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>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Niraj Tolia
  • 541
  • 5
  • 5