1

I have a java code where I want to use Guava libraries CharMatcher function. I downloaded it, but can't able to add it to my existing code. Can anybody help me how to add that .jar file to the java code (step-by-step). I am using JDK 1.6. Thanks in advance.

I am using the following command:

enter image description here

And Used follwing code:

public class Test {

 public static void main(String[] args) {

try {

 String key = "hello";
 Multimap<String, Integer> myMap = HashMultimap.create();
 myMap.put(key, 1);
 myMap.put(key, 5000);
 System.out.println(myMap.get(key));

}

 catch (FileNotFoundException e) {
  System.out.println(e);
  } catch (Exception e) {
  System.out.println(e);
  }


}
}

I store Guava Library and Test.java in C:\Program Files\Java\jdk1.6.0_25\bin . Can anybody help me where is the problem because it is showing errors.

alessandro
  • 1,681
  • 10
  • 33
  • 54
  • 2
    duplicate http://stackoverflow.com/questions/1064481/newbie-question-how-to-include-jar-files-when-compiling – scibuff Mar 23 '12 at 14:03
  • 2
    Step-by-step instructions would obviously depend on what programming environment you're using: NetBeans, Eclipse, Notepad, BlueJay... what? – Ernest Friedman-Hill Mar 23 '12 at 14:05
  • @ErnestFriedman-Hill Sorry, I am not using NetBeans, Eclipse etc. I am using Javac (JDk 1.6) to run it. – alessandro Mar 23 '12 at 14:15
  • Well, then see the comment about this being a duplicate of http://stackoverflow.com/questions/1064481, because it is! – Ernest Friedman-Hill Mar 23 '12 at 14:28
  • @ErnestFriedman-Hill I used that command but showing above described errors. – alessandro Mar 23 '12 at 14:59
  • OK, now that we see that: this actually has nothing to do with the classpath or including the jar. The classes you're using are in the package `com.google.common.collect`. You have to do, for example, `import com.google.common.collect.HashMultimap;` at the top of your Java file. – Ernest Friedman-Hill Mar 23 '12 at 17:06

3 Answers3

7

Suppose you are using Eclipse as IDE, here is the picture: enter image description here

EDIT: if you don't use any IDE, you just must use CLASSPATH variable:

javac -classpath path_to_your_jar/yourjar.jar your_code.java

'-classpath' or '-cp' switch gives you possibility to tell javac where your libraries are located. See link for further reference.

Aleksandr Kravets
  • 5,750
  • 7
  • 53
  • 72
3

If your just doing command line, you can either modify the classpath environment variable, or pass the -classpath argument on the command line:

Windows: set CLASSPATH=%CLASSPATH%;\guava\install\dir\guava.jar
*nix export CLASSPATH=$CLASSPATH:/guava/install/dir/guava.jar

OR

javac -classpath guava\install\dir\guava.jar ...
java -classpath guava\install\dir\guava.jar ...
Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
Mike
  • 3,186
  • 3
  • 26
  • 32
0

Simply add it to your classpath:

  • Copy .jar in your project-folder
  • Refresh your project-explorer in eclipse (i hope you're using eclipse)
  • rightclick .jar
  • Buildpath -> add to build path

I hope this helps you, i'm not really sure if this is what you are asking for ;)