我收到“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

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

问题描述

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

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.

当我添加其他库时,它们运行良好,我可以毫无问题地使用它们,但是当我尝试创建 JSON 对象时,出现了标题错误.我已经查看了其他有关此错误的问题,但找不到不涉及我尝试过的内容或不相关的内容的解决方案.

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();

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

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?

更多信息

引用 com.google.gson.GSON 的代码被 jsp 用作 bean.这个函数是从jsp调用的,一执行就报错.函数的第一行是:

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.

谢谢

推荐答案

如果是 JSP/Servlet web 应用程序,你只需要将 3rd party JAR 文件放到 /WEB-INF/lib 文件夹中.如果项目是一个动态 Web 项目,那么 Eclipse 也会自动负责正确设置构建路径.您不需要需要摆弄 Eclipse 构建路径.不要忘记全部撤消.

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天全站免登陆