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?
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.)
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:
Preferences -> Java -> Build Path -> User Libraries
New...
, enter a name (e.g. 'Guava')Add JARs...
and search for the JAR files you downloadedguava-r07.jar
. Select that in this dialog, preferably after moving it to a more desirable location)Source Attachment -> Edit...
, and find the path to the file guava-src-r07
.javadoc
folder from the guava distributionOK
.Now you should be all set, you can select the user library and add it to a project of your choice.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With