如何在 Java 中安装 GSON 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2774340/
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-13 12:42:52  来源:igfitidea点击:

How do I install the GSON module in Java

javamodulegson

提问by Mridang Agarwalla

I downloaded the Google JSON module a.k.a GSON. I'm ona windows system Could you tell me how to install the GSON module? I extracted the JAR into the following folder which was in my classpath:

我下载了 Google JSON 模块又名 GSON。我是windows系统 你能告诉我如何安装GSON模块吗?我将 JAR 解压缩到我的类路径中的以下文件夹中:

C:\Program Files\Java\jdk1.6.0_07\lib

..but when i type:

..但是当我输入:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

I still get a module not found error.

我仍然收到未找到模块的错误。

What am I doing wrong?

我究竟做错了什么?

Thanks.

谢谢。

采纳答案by BalusC

  1. You should not extract the JAR.
  2. You should not put 3rd party libraries in JDK/lib.
  1. 您不应该提取 JAR。
  2. 你不应该把 3rd 方库放在JDK/lib.

You need to specify it in the classpath using -cpor -classpathargument. E.g.

您需要使用-cp-classpath参数在类路径中指定它。例如

java -cp .;/path/to/gson.jar com.example.MyClass

To save the time in typing it everytime, use a .batfile to execute it.

为了节省每次输入的时间,请使用.bat文件来执行它。

If you're actually using an IDE like Eclipse, then you can also just rightclick the Java project, choose Build Pathand then add it as new Library. Eclipse will then automatically take it in both the compiletime and runtime classpath.

如果您实际上使用的是 Eclipse 之类的 IDE,那么您也可以右键单击 Java 项目,选择Build Path,然后将其添加为 new Library。然后 Eclipse 将自动在编译时和运行时类路径中使用它。