我得到“java.lang.ClassNotFoundException:com.google.gson.Gson”即使在我的类路径中定义了错误 [英] I am getting "java.lang.ClassNotFoundException: com.google.gson.Gson" error even though it is defined in my classpath

查看:148
本文介绍了我得到“java.lang.ClassNotFoundException:com.google.gson.Gson”即使在我的类路径中定义了错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一些使用gson-1.6.jar的JSON对象字符串,我将它放在与其他.jars相同的位置,并将其添加到我的eclipse中的构建路径。



当我添加它们时,其他库工作正常,我可以使用它们没有任何问题,但是当我尝试创建JSON对象时,我会得到标题错误。我已经看过这个错误的其他问题,但是我找不到一个解决方案,没有涉及到我曾经尝试过或者无关的东西。



我使用以下方式将其导入顶部:

  import com.google.gson.Gson; 

然后在静态函数中使用它,如下所示:

  Gson g = new Gson(); 

这是我的eclipse生成的类路径文件[路径]替代实际路径:

 <?xml version =1.0encoding =UTF-8?> 
< classpath>
< classpathentry kind =srcpath =src/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER / org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType / jre6>
<属性>
< attribute name =owner.project.facetsvalue =java/>
< / attributes>
< / classpathentry>
< classpathentry kind =conpath =org.eclipse.jst.server.core.container / org.eclipse.jst.server.tomcat.runtimeTarget / Apache Tomcat v5.5>
<属性>
< attribute name =owner.project.facetsvalue =jst.web/>
< / attributes>
< / classpathentry>
< classpathentry kind =conpath =org.eclipse.jst.j2ee.internal.web.container/>
< classpathentry kind =conpath =org.eclipse.jst.j2ee.internal.module.container/>
< classpathentry kind =libpath =[Path] /jabbabase-ws-jwsdp-client-2.4.7.jar/>
< classpathentry kind =libpath =[Path] /log4j-1.2.16.jar/>
< classpathentry kind =libpath =[Path] /gson-1.6.jar/>
< classpathentry kind =outputpath =build / classes/>
< / classpath>

以及我的构建路径窗口:



在这一点上我有点迷失了。我试过谷歌和帖子基本上只是说添加到你的构建路径。任何人都有任何想法?



编辑:更多信息



引用com.google.gson.GSON的代码是用作jsp的bean。该函数从jsp调用,一旦执行该函数就会发生错误。函数的第一行是:

  Gson g = new Gson(); 

似乎编译和部署正常,但执行时,我收到错误消息。 / p>

谢谢

解决方案

如果是JSP / Servlet webapplication,只需要在 / WEB-INF / lib 文件夹中删除第三方JAR文件。如果项目是动态Web项目,那么Eclipse将会自动关注设置正确的构建路径。你不要 需要解决Eclipse的buildpath。不要忘记全部撤消。


I'm trying to parse some JSON object strings that I'm getting using gson-1.6.jar I have placed it in the same location as my other .jars and have added it to my buildpath in eclipse.

The other libraries worked fine when I added them and I can use them without any issues, but when I try to create the JSON object, I get the titular error. I've looked through the other questions with this error, but I couldn't find a solution that didn't involve something that I've tried or something unrelated.

I import it near the top using:

import com.google.gson.Gson;

Then use it later in a static function like so:

Gson g = new Gson();

Here is my eclipse generated classpath file [Path] substituted for actual path:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre6">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v5.5">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="lib" path="[Path]/jabbabase-ws-jwsdp-client-2.4.7.jar"/>
    <classpathentry kind="lib" path="[Path]/log4j-1.2.16.jar"/>
    <classpathentry kind="lib" path="[Path]/gson-1.6.jar"/>
    <classpathentry kind="output" path="build/classes"/>
</classpath>

as well as my build path window:

I'm a little lost at this point. I've tried Google and the posts basically just say to add to your build path. Anybody have any ideas?

Edit: More Info

The code referencing com.google.gson.GSON is used as a bean by a jsp. This function is called from the jsp and the error occurs as soon as the function is executed. The first line in the function is:

Gson g = new Gson();

It seems to compile and deploy fine, but when it is executed, I get the error message.

Thanks

解决方案

In case of a JSP/Servlet webapplication, you just need to drop 3rd party JAR files in /WEB-INF/lib folder. If the project is a Dynamic Web Project, then Eclipse will automatically take care about setting the buildpath right as well. You do not need to fiddle with Eclipse buildpath. Don't forget to undo it all.

这篇关于我得到“java.lang.ClassNotFoundException:com.google.gson.Gson”即使在我的类路径中定义了错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆