Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install the GSON module in Java

Tags:

java

module

gson

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.

like image 556
Mridang Agarwalla Avatar asked May 05 '10 14:05

Mridang Agarwalla


People also ask

What is Gson class in Java?

Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.


1 Answers

  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.

like image 93
BalusC Avatar answered Oct 21 '22 20:10

BalusC