Include gson in my java program

classpath, gson, java

Solution

This answer assumes you are using the `javac` and `java` commands in a *nix environment.

To compile your code you need to put the jar and your Java files on to the classpath with the `-cp` flag. For this small example, you really only need to provide the Java file that has your main method. This is because the compiler will look for any Java files it needs to compile along with `YourClass.java` by searching for them on the classpath.

javac -cp /path/to/java/files:/path/to/gson-2.2.1.jar YourClass.java

To run your code you need to do the same thing but only refer to the class with your main method.

java -cp /path/to/class/files:/path/to/gson-2.2.1.jar YourClass

Keep in mind that the `/path/to/java/files` and `/path/to/class/files` directories must point to the root directory of your packages (if you using packages, which you should).

Problem

So far I have only used standard libraries in my programs. I'm just making a simple console application and I'm not using any IDE, just simple text editor (because I don't need anything more complex at the moment). I don't know where to put jar file I've downloaded and I also don't know how to call it correctly. I've read something about include path? But I'm not sure if I understand. I just have simple folder structure like so: - Project - Class.java - Class.class - gson-2.2.1.jar I've tried with this: ``` import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonParser; ``` But I get that it doesn't exist.

Original source