【问题标题】:package com.google.gson does not exist [duplicate]包 com.google.gson 不存在 [重复]
【发布时间】:2014-11-25 18:13:14
【问题描述】:

我正在使用以下程序:特别是 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 程序代码以及我创建的成分类时,都会发生这两个错误。我真正关心的是如何有效地处理这个问题......

【问题讨论】:

  • 1) import 语句可以使用类型的简单名称。它实际上并没有 import 任何东西。您必须将库添加到编译(和运行时)类路径。
  • 2) 请说明发生的位置。
  • 当我在 CMD 中编译它时,我的 GSON java 代码的第 2 行和第 13 行出现错误。因为代码已经确信运行程序所需的 GSON 包不存在,这意味着它也找不到符号或获取依赖该代码的方法所需的类。简而言之,这就是我的问题......

标签: java json gson


【解决方案1】:

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

javac -classpath <LOCATION_TO_GSON JAR FILE>  *.java

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

【讨论】:

  • 我知道我迟到了,因为我试图在这里遵循您的完美建议...我确定这听起来像 javac -classpath CLASSPATH%=c:\progs\tomcat8\lib *.java OR javac -classpath CLASSPATH%=c:\progs\tomcat8\webapps\webserv1\WEB-INF\lib*.java... 听起来合法;遗憾的是,尽管我同时编译了我提供的两个 java 代码,但我一直在 gettign 包不存在错误...
  • 从您的评论中,我看到了一个问题。它应该是这样的: javac -classpath %CLASSPATH%;/gson-2.3.jar *.java
  • 谢谢你 - 另外,很抱歉这么晚才回复(我必须工作......)但我会试试这个。
  • 我尝试了你的想法 - 我一直得到的只是一个无效的标志错误。我在这里做错了什么?!
  • 你能把错误贴在这里吗?
【解决方案2】:

运行时需要Tomcat classpath 中的Gson jar 文件,编译时需要classpath of your development environment 中的Gson jar 文件。

【讨论】:

  • 你一直都是对的——但我还需要一个位于 c:\progs 中的 gson jar 文件......一切都开始工作了。我还听取了您的建议,将 gson 放入我的主库中的 tomcat8 和我的虚拟机上的 webserv1/lib 文件夹中。也设法在那里工作。谢谢你。
  • osx 的特定示例(也可能是 linux):转到 mvnrepository.com/artifact/com.google.code.gson/gson 并下载最新的 jar(到您运行 javac 时所在的同一文件夹中),例如在central.maven.org/maven2/com/google/code/gson/gson/2.6.2/…,然后用javac -classpath "`pwd`/`ls gson*.jar`" mycode.java编译
猜你喜欢
  • 2021-02-26
  • 2011-12-01
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 2022-01-17
  • 2019-07-01
  • 2021-10-23
相关资源
最近更新 更多