I'm working with the following programs: Specifically Tomcat 8, VirtualBox virtual machine, & Gson 2.3.1
I'm trying to test out a method in Gson that allows you marshall/unmarshall Java objects into the JSON format.
This is the code I am using this for (in order for it to compile properly, both classes of java code need to be compiling at the same time...:
Most importantly is my code for testing the JSON conversion method:
package com.cs330;
import com.google.gson.Gson;
public class IngredientProto
{
public static void main (String[] args)
{
Ingredient neo = new Ingredient(6, "cheddar cheese", "cheese");
System.out.println("Object confirmed: " + neo.toString());
//New GSON object for marshalling...
Gson neoIngredient = new Gson();
//Use said object to create the JSON formatted String for this Ingredient (Object).
String jsonThis = neoIngredient.toJson(neo);
//Printing out the new object (Ingredient)...
System.out.println ("Object formatted to JSON: " + jsonThis);
//Convert Back via unmarshalling. NOW!
Ingredient orga = neoIngredient.fromJson(jsonThis, Ingredient.class);
//Print Again!
System.out.println("Reverting back into Object variable: " + orga.toString());
}
}
Here, for reference, is my code for the Ingredient Class I'm working with...
package com.cs330;
import java.util.Scanner;
public class Ingredient //implements Comparable<Ingredient>
{
private int ingredientID;
private String ingredientName;
protected String ingredientType;
//DEFAULT CONSTRUCTOR
Ingredient()
{
this.ingredientID = 0;
this.ingredientName = "No Ingredient";
this.ingredientType = "No Type";
}
//CONSTRUCTOR 1 - Parameters for Name, ID, and Type.
Ingredient(int ingredientID, String ingredientName, String ingredientType)
{
setIngredientID(ingredientID);
setIngredientName(ingredientName);
setIngredientType(ingredientType);
}
//CONSTRUCTOR 2 - Parameters for Name and Type.
Ingredient(String ingredientName, String ingredientType)
{
this.ingredientID = 0;
setIngredientName(ingredientName);
setIngredientType(ingredientType);
}
//Getters and Setters Go here.
//TO STRING
public String toString()
{
String results = " ";
results = this.ingredientID + ": " + this.ingredientName
+ " (" + this.ingredientType + ") ";
return results;
}//END toSTRING
//...Main for testing...
}//END PROGRAM
The errors I keep getting involve the following whenever I try to compile them in CMD on my virtual machine:
error: package com.google.gson does not exist- I have import com.google.gson.Gson where it needs to be in my program, yet I keep getting this error my code has nothing wrong with it...error: class, interface, or enum expected- I actually add package com.google.gson, and I get this error despite everything else being right.
Both errors happen when I try to test the Gson program code out, alongside the Ingredient class I created. My real concern is how I can effectively deal with this...
2 Answers
While compiling in the Command Prompt use the -cp or -classpath to set your gson.jar in your classpath. Something like this:
javac -classpath <LOCATION_TO_GSON JAR FILE> *.java
During the runtime check that gson library is present in your classpath (either WEB-INF/lib or \apache-tomcat-7.0.42\lib)
You need the Gson jar file in the Tomcat classpath during runtime and in the classpath of your development environment for compilation.