com.google.gson 包不存在

Sho*_*kew 11 java json gson

我正在使用以下程序:具体是 Tomcat 8、VirtualBox 虚拟机和 Gson 2.3.1

我正在尝试在 Gson 中测试一种方法,该方法允许您将 Java 对象编组/解组为 JSON 格式。

这是我使用它的代码(为了使其正确编译,两个java代码类需要同时编译......:

最重要的是我用于测试 JSON 转换方法的代码:

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());
       }
      }
Run Code Online (Sandbox Code Playgroud)

作为参考,这里是我正在使用的成分类的代码......

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
Run Code Online (Sandbox Code Playgroud)

每当我尝试在虚拟机上的 CMD 中编译它们时,我不断遇到的错误涉及以下内容:

  1. error: package com.google.gson does not exist- 我已经导入 com.google.gson.Gson ,它需要在我的程序中,但我不断收到此错误,我的代码没有任何问题......
  2. error: class, interface, or enum expected- 我实际上添加了包 com.google.gson,尽管其他一切都正确,但我还是收到了此错误。

当我尝试测试 Gson 程序代码以及我创建的 Ingredient 类时,这两个错误都会发生。我真正关心的是如何有效地处理这个问题......

小智 6

在命令提示符下编译时,使用 -cp 或 -classpath 在类路径中设置 gson.jar。像这样的东西:

javac -classpath <LOCATION_TO_GSON JAR FILE>  *.java
Run Code Online (Sandbox Code Playgroud)

在运行时检查 gson 库是否存在于您的类路径中(WEB-INF/lib\apache-tomcat-7.0.42\lib


lot*_*har -1

您需要在运行时将 Gson jar 文件放在Tomcat 类路径中,并在您的开发环境的类路径中进行编译。