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.
This answer assumes you are using the
javacandjavacommands in a *nix environment.To compile your code you need to put the jar and your Java files on to the classpath with the
-cpflag. 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 withYourClass.javaby searching for them on the classpath.To run your code you need to do the same thing but only refer to the class with your main method.
Keep in mind that the
/path/to/java/filesand/path/to/class/filesdirectories must point to the root directory of your packages (if you using packages, which you should).