tags:

views:

299

answers:

1

Hi guys,

I downloaded the Google JSON module a.k.a GSON. I'm ona windows system Could you tell me how to install the GSON module? I extracted the JAR into the following folder which was in my classpath:

C:\Program Files\Java\jdk1.6.0_07\lib

..but when i type:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

I still get a module not found error.

What am I doing wrong?

Thanks.

+3  A: 
  1. You should not extract the JAR.
  2. You should not put 3rd party libraries in JDK/lib.

You need to specify it in the classpath using -cp or -classpath argument. E.g.

java -cp .;/path/to/gson.jar com.example.MyClass

To save the time in typing it everytime, use a .bat file to execute it.

If you're actually using an IDE like Eclipse, then you can also just rightclick the Java project, choose Build Path and then add it as new Library. Eclipse will then automatically take it in both the compiletime and runtime classpath.

BalusC
This is what i typed:java -cp .;c:\gson-1.3.jar com.google.gson.GsonI got the error - "Exception in thread "main" java.lang.NoSuchMethodError: main".
Mridang Agarwalla
`com.google.gson.Gson` indeed doesn't have a main method. It's not an executabele. You need to create your own class with a main method which in turn imports, uses and executes the normal Gson methods. Note: the command line in my answer is just meant to demonstrate how to execute YOUR class which in turn is dependent on the Gson library. It is not a command to "install" Gson or so. All you need to do is to specify it in the classpath when compiling and executing YOUR Java program. The above line exactly does that for executing. For compiling, just replace `java` by `javac`. Or use an IDE.
BalusC
I have a working project in which I'm trying to use GSON. I'm aware that GSON is a library and not an executable but I guess I would have to have it installed somewhere/somehow so that I can import it. This is hwere the problem lies as I don't know here it should be placed so that i can import it. Thank you.
Mridang Agarwalla
Thus .. Just specify it in the classpath during compiling or executing of YOUR program. The command line in my answer does exactly that.
BalusC