This answer assumes that you are using javac
and java
commands in a * nix environment.
To compile your code, you must put the jar and your Java files in the classpath with the -cp
flag. For this small example, you really need to provide a Java file that has your main method. This is because the compiler will look for any Java files that it should compile with YourClass.java
by looking for them in the class path.
javac -cp /path/to/java/files:/path/to/gson-2.2.1.jar YourClass.java
To run the code, you need to do the same, but access the class only using the main method.
java -cp /path/to/class/files:/path/to/gson-2.2.1.jar YourClass
Keep in mind that the directories /path/to/java/files
and /path/to/class/files
should point to the root directory of your packages (if you use the packages you need).
source share