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
Getting java.lang.NoClassDefFoundError while using Google Gson library
提问by Rajath
I am using Gson library
to convert objects to Json
and vice versa. I have copied the jars
of that library into a new folder lib
and 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 servlet
using Apache HttpClient's HttpPost()
.
我有一个主类,我使用toJson()
如下所示将对象转换为 Json并将其发送到servlet
using Apache HttpClient's HttpPost()
。
Gson gson= new Gson();
String json = gson.toJson(names);
But in the servlet
I am not able to convert the Json
into Object
using fromJson()
when I execute the following code.
但是在执行以下代码时,我servlet
无法将其转换Json
为Object
using 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 jars
into the WebContent/WEB-INF/lib
folder instead of a new folder called lib
?
知道为什么会发生这种情况吗?我应该将 复制jars
到WebContent/WEB-INF/lib
文件夹而不是一个名为 的新文件夹中lib
吗?
采纳答案by Rajath
Okay, I figured out the answer using this post. Please read the comments in the first answer.
好的,我用这篇文章找到了答案。请阅读第一个答案中的评论。
我收到“java.lang.ClassNotFoundException: com.google.gson.Gson”错误,即使它是在我的类路径中定义的
I just had to place the external jars
into my WebContent/WEB-INF/lib
folder and Eclipse
would take care of the build path
itself.
我不得不外部放置jars
到我的WebContent/WEB-INF/lib
文件夹,并Eclipse
会采取照顾build path
自己。