java - com.google.gson 包不存在

标签 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());
       }
      }

这里是我正在使用的成分类的代码,仅供引用...

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

每当我尝试在虚拟机上的 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 类时,会发生这两个错误。我真正关心的是如何有效地处理这个问题......

最佳答案

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

javac -classpath <LOCATION_TO_GSON JAR FILE>  *.java

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

关于java - com.google.gson 包不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27133797/

相关文章:

android - Retrofit 2示例教程但GsonConverterFactory显示错误 "Cannot resolve symbol"

java.lang.ExceptionInInitializerError | java.lang.ExceptionInInitializerError | java.lang.ExceptionInInitializerError由 : com. b.a.c.b 引起: 'com.b.a.b.a' 需要 'sequence' 属性

java - MockMVC 对于对象列表返回 406(对于一个对象测试有效),为什么?

javascript - 从 JSON 构建下拉列表,获取空白选项项

java - 如何转义 MessageFormat 模式字符串中的 { 字符?

java.net.SocketException : recvfrom failed: EBADF (Bad file descriptor) Android

javascript - delete object[element] 不会删除,而是将其设置为 null ....如何删除它而不是使其为 null?

java - 使用Gson解析对象数组

java - 如何正确等待对象被实例化

java - 如何处理每 10 秒通过 ftp 接收的 50 k 个文件