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

发布于 2024-10-16 20:34:58 字数 1960 浏览 3 评论 0原文

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

当我添加其他库时,它们工作得很好,并且我可以毫无问题地使用它们,但是当我尝试创建 JSON 对象时,我收到标题错误。我已经查看了包含此错误的其他问题,但找不到不涉及我尝试过的内容或不相关内容的解决方案。

我使用以下命令在顶部附近导入它:

import com.google.gson.Gson;

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

Gson g = new Gson();

这是我的 eclipse 生成的类路径文件 [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>

以及我的构建路径窗口: Build Path Screen

此时我有点迷失。我试过谷歌,帖子基本上只是说添加到你的构建路径中。有人有什么想法吗?

编辑:更多信息

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

Gson g = new Gson();

它似乎编译和部署得很好,但是当它执行时,我收到错误消息。

谢谢

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:
Build Path Screen

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

抚笙 2024-10-23 20:34:58

对于 JSP/Servlet Web 应用程序,您只需将第 3 方 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.

清醇 2024-10-23 20:34:58

我遇到了同样的问题并尝试了上述解决方案,但没有一个对我有用。然后我尝试了以下步骤,问题得到解决:

  • 转到项目属性。
  • 转到 Java 构建路径选项。
  • 然后添加 *.jar 文件作为外部 jar。
  • 然后转到订单和导出选项并选择项目的库和 jar。
  • 保存当前更改并清理项目并再次运行项目。

I faced same problem and tried above solutions but none of them worked for me. Then I tried following steps and the problem was solved:

  • Go to the project properties.
  • Go to Java Build Path option.
  • Then add *.jar file as external jar.
  • Then go to the order and export option and select the libraries and jars of the project.
  • save the current changes and clean the project and run the project again.
骷髅 2024-10-23 20:34:58

单击“部署程序集”(位于显示为活动状态的“Java 构建路径”正上方)并确保在那里看到 json-lib-2.4-jdk15.jar。

通常,您应该将其添加到构建路径并从项目中导出。导出后,您将看到 WTP 警告,表明它不是部署的一部分。选择快速修复选项并将其添加到您的部署路径。

Click on Deployment Assembly ( right above Java Build Path that you show as active ) and make sure that you see json-lib-2.4-jdk15.jar there.

Usually, you should add it to your build path and export it from your project. Once it's exported you will see the WTP warning that it's not a part of the deployment. Choose the Quick Fix option and add it to your deployment path.

各空 2024-10-23 20:34:58

我只是遇到了同样的问题...

要解决问题(至少在我的情况下),请确保您已在捆绑包类路径中包含 lib 文件夹:

Manifest-Version: 1.0
... 
Bundle-ClassPath: lib/gson-1.6.jar,
 .
...

或者如果您想在文件夹中包含所有 jar:

Bundle-ClassPath: lib/

您仍然需要将 jar 文件放置在 java 构建路径上,如上所示。然后您导入的 jar 应该出现在“Referenced Libraries”文件夹中

I was just having the same issue...

To resolve the problem (at least in my case) ensure you have included the lib folder in your bundle classpath:

Manifest-Version: 1.0
... 
Bundle-ClassPath: lib/gson-1.6.jar,
 .
...

Or if you want to include all jar's in the folder:

Bundle-ClassPath: lib/

You will still need to place the jar files on the java build path as shown above. Then your imported jar's should appear in the folder "Referenced Libraries"

硪扪都還晓 2024-10-23 20:34:58

对我来说,通过将 jar 添加到我的 APACHE/TOMCAT/lib 文件夹中,问题得到了解决! 。

Well for me the problem got resolved by adding the jars in my APACHE/TOMCAT/lib folder ! .

故事还在继续 2024-10-23 20:34:58

您可以在 pom.xml 文件中包含 Maven 依赖项,如下所示

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
  <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
  </dependency>

you can include maven dependency like below in your pom.xml file

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
  <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
  </dependency>
场罚期间 2024-10-23 20:34:58

如果您正在开发动态 Wb 应用程序,则将 .jar 添加到 java 构建路径后,您还必须执行以下操作:

Project ->属性->部署组装->添加...-> Java 构建路径条目 ->下一步->选择 JAR 文件并单击完成。应用并关闭。

这对我有帮助。

If you are working on a Dynamic Wb App, after adding .jar to the java build path, you also have to do:

Project -> Properties -> Deployment Assembly -> Add... -> Java Build Path Entries -> Next -> select the JAR file and click Finish. Apply and Close.

That helps me.

メ斷腸人バ 2024-10-23 20:34:58

我在 Eclipse 环境中开发 KNIME Node/plugin 时遇到了同样的问题。解决方案不仅是将 gson.jar 作为外部 JAR 添加到构建路径,还需要转到 plugin.xml,然后转到“依赖项”选项卡并将 com.google.gson 添加为一个必需的插件。

I had the same problem when developing a KNIME Node/plugin in the Eclipse environment. The solution was not only to add the gson.jar as an externar JAR to the build path, it was also required to go to plugin.xml, then the Dependencies tab and add com.google.gson as a required plugin.

优雅的叶子 2024-10-23 20:34:58

在“标记”选项卡中执行“快速修复”。

图片来源metamug.com

参考:https://metamug.com/blog/eclipse-gson-class-not-found< /a>

Do the Quick fix in the Markers tab.

Image Source metamug.com

Reference: https://metamug.com/blog/eclipse-gson-class-not-found

深海夜未眠 2024-10-23 20:34:58

我在 Eclipse 中构建和运行时遇到了上述错误,除了这个错误之外,一切似乎都很好。然而,我发现 Maven 构建失败,我需要将 Gson 包含在我的 pom.xml 中。修复 pom.xml 后,一切就位了。

I ran into the above error when building and running inside Eclipse, where everything seemed to be fine, with the exception of this error. However, I discovered that a Maven build failed and that I needed to include Gson in my pom.xml. After fixing the pom.xml, everything fell into place.

沩ん囻菔务 2024-10-23 20:34:58

如果它可以帮助任何人排除故障,因为我作为第一次用户在 Eclipse 中浪费了 30 分钟的时间 - 我已将 Gson jar 添加到构建路径库的“ModulePath”部分(与 JRE 系统库一起)。我需要将其添加到“类路径”中才能解决此错误。

In case it helps anyone troubleshoot as I just wasted 30 minutes on this in Eclipse as a first time user - I had added the Gson jar to the 'ModulePath' section of build path libraries (alongside JRE system library). I needed to add it to the 'Classpath' to resolve this error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文