Java 包 com.google.gson 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27133797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:52:40  来源:igfitidea点击:

package com.google.gson does not exist

javajsongson

提问by Shokew

I'm working with the following programs: Specifically Tomcat 8, VirtualBox virtual machine, & Gson 2.3.1

我正在使用以下程序:特别是 Tomcat 8、VirtualBox 虚拟机和 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.

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

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...:

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

Most importantly is my code for testing the JSON conversion method:

最重要的是我用于测试 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());
       }
      }

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:

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

  1. 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...
  2. error: class, interface, or enum expected- I actually add package com.google.gson, and I get this error despite everything else being right.
  1. error: package com.google.gson does not exist- 我在程序中需要的地方导入了 com.google.gson.Gson,但我不断收到此错误,我的代码没有任何问题...
  2. error: class, interface, or enum expected- 我实际上添加了包 com.google.gson,尽管其他一切都正确,但我还是收到了这个错误。

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...

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

回答by lothar

You need the Gson jar file in the Tomcat classpathduring runtime and in the classpath of your development environmentfor compilation.

运行时需要Tomcat类路径中的Gson jar文件,以及编译环境类路径中的Gson jar文件。

回答by Bhaskara

While compiling in the Command Prompt use the -cp or -classpath to set your gson.jar in your classpath. Something like this:

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

javac -classpath <LOCATION_TO_GSON JAR FILE>  *.java

During the runtime check that gson library is present in your classpath (either WEB-INF/libor \apache-tomcat-7.0.42\lib)

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