eclipse 使用 Google Gson 库时出现 java.lang.NoClassDefFoundError

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

Getting java.lang.NoClassDefFoundError while using Google Gson library

eclipseclasspathgsonapache-httpclient-4.xapache-httpcomponents

提问by Rajath

I am using Gson libraryto convert objects to Jsonand vice versa. I have copied the jarsof that library into a new folder liband added it to the build path for the project in my Eclipse IDE.

我正在使用Gson library将对象转换为对象,Json反之亦然。我已jars将该库的复制到一个新文件夹中lib,并将其添加到我的Eclipse IDE.

I have one main class where I convert an object into Json using toJson()shown below and send it to a servletusing Apache HttpClient's HttpPost().

我有一个主类,我使用toJson()如下所示将对象转换为 Json并将其发送到servletusing Apache HttpClient's HttpPost()

Gson gson= new Gson();
String json = gson.toJson(names);

But in the servletI am not able to convert the Jsoninto Objectusing fromJson()when I execute the following code.

但是在执行以下代码时,我servlet无法将其转换JsonObjectusing fromJson()

Gson gson = new Gson();
Names names = gson.fromJson(s, Names.class);

It is throwing the following exception:

它抛出以下异常:

java.lang.NoClassDefFoundError: com/google/gson/Gson

Any idea why that could be happening? Should I have copied the jarsinto the WebContent/WEB-INF/libfolder instead of a new folder called lib?

知道为什么会发生这种情况吗?我应该将 复制jarsWebContent/WEB-INF/lib文件夹而不是一个名为 的新文件夹中lib吗?

采纳答案by Rajath

Okay, I figured out the answer using this post. Please read the comments in the first answer.

好的,我用这篇文章找到了答案。请阅读第一个答案中的评论。

I am getting "java.lang.ClassNotFoundException: com.google.gson.Gson" error even though it is defined in my classpath

我收到“java.lang.ClassNotFoundException: com.google.gson.Gson”错误,即使它是在我的类路径中定义的

I just had to place the external jarsinto my WebContent/WEB-INF/libfolder and Eclipsewould take care of the build pathitself.

我不得不外部放置jars到我的WebContent/WEB-INF/lib文件夹,并Eclipse会采取照顾build path自己。