简体   繁体   English

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

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.我正在尝试解析我使用 gson-1.6.jar 获得的一些 JSON 对象字符串,我将它放在与其他 .jar 相同的位置,并将它添加到我在 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.当我添加其他库时,它们运行良好,我可以毫无问题地使用它们,但是当我尝试创建 JSON 对象时,出现了标题错误。 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:这是我的 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>

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.引用 com.google.gson.GSON 的代码被 jsp 用作 bean。 This function is called from the jsp and the error occurs as soon as the function is executed.这个函数是从jsp调用的,一执行就报错。 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.如果是 JSP/Servlet web 应用程序,您只需要将 3rd 方 JAR 文件放到/WEB-INF/lib文件夹中。 If the project is a Dynamic Web Project , then Eclipse will automatically take care about setting the buildpath right as well.如果项目是Dynamic Web Project ,那么 Eclipse 也会自动负责正确设置构建路径。 You do not need to fiddle with Eclipse buildpath.不需要使用Eclipse构建路径摆弄。 Don't forget to undo it all.不要忘记全部撤消。

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.转到 Java 构建路径选项。
  • Then add *.jar file as external jar.然后添加*.jar文件作为外部 jar。
  • Then go to the order and export option and select the libraries and jars of the project.然后转到 order 和 export 选项并选择项目的库和 jars。
  • save the current changes and clean the project and run the project again.保存当前更改并清理项目并再次运行项目。

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.单击Deployment Assembly (在您显示为活动的Java Build Path正上方)并确保您在那里看到 json-lib-2.4-jdk15.jar。

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.导出后,您将看到 WTP 警告,指出它不是部署的一部分。 Choose the Quick Fix option and add it to your deployment path.选择Quick Fix选项并将其添加到您的部署路径中。

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:要解决该问题(至少在我的情况下),请确保您已将 lib 文件夹包含在您的包类路径中:

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

Or if you want to include all jar's in the folder:或者,如果您想在文件夹中包含所有 jar:

Bundle-ClassPath: lib/

You will still need to place the jar files on the java build path as shown above.您仍然需要将 jar 文件放在 java 构建路径上,如上所示。 Then your imported jar's should appear in the folder "Referenced Libraries"然后你导入的 jar 应该出现在文件夹“Referenced Libraries”中

Well for me the problem got resolved by adding the jars in my APACHE/TOMCAT/lib folder !对我来说,通过在我的 APACHE/TOMCAT/lib 文件夹中添加 jars 解决了问题! . .

I had the same problem when developing a KNIME Node/plugin in the Eclipse environment.我在 Eclipse 环境中开发 KNIME 节点/插件时遇到了同样的问题。 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.解决方案不仅是将 gson.jar 作为外部 JAR 添加到构建路径中,还需要转到plugin.xml ,然后是 Dependencies 选项卡并添加 com.google.gson 作为必需的插件。

Do the Quick fix in the Markers tab.在标记选项卡中执行快速修复。

图片来源 metamug.com

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

I ran into the above error when building and running inside Eclipse, where everything seemed to be fine, with the exception of this error.我在 Eclipse 中构建和运行时遇到了上述错误,除了这个错误之外,一切似乎都很好。 However, I discovered that a Maven build failed and that I needed to include Gson in my pom.xml.但是,我发现 Maven 构建失败,我需要在 pom.xml 中包含 Gson。 After fixing the pom.xml, everything fell into place.修复了 pom.xml 之后,一切就都到位了。

you can include maven dependency like below in your pom.xml file您可以在 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> <!-- 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>

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).如果它可以帮助任何人进行故障排除,因为我只是作为第一次用户在 Eclipse 中浪费了 30 分钟 - 我已将 Gson jar 添加到构建路径库的“ModulePath”部分(与 JRE 系统库一起)。 I needed to add it to the 'Classpath' to resolve this error.我需要将它添加到“类路径”以解决此错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 java.lang.ClassNotFoundException: com.google.gson.Gson 对于 maven 项目 - Getting java.lang.ClassNotFoundException: com.google.gson.Gson for maven project java.lang.ClassNotFoundException:com.google.gson.Gson,尽管Apache服务器中具有JAR - java.lang.ClassNotFoundException: com.google.gson.Gson despite having the JAR in the Apache server “java.lang.ClassNotFoundException: com.google.gson.Gson”但在 Eclipse 本地项目的构建路径中 - "java.lang.ClassNotFoundException: com.google.gson.Gson" but have it in build path on local project on Eclipse 获取java.lang.NoClassDefFoundError:com / google / gson / Gson - Getting java.lang.NoClassDefFoundError: com/google/gson/Gson 为什么会出现错误“ java.lang.ClassNotFoundException”? - Why am I getting an error “java.lang.ClassNotFoundException”? 为什么在编译程序后出现“ java.lang.ClassNotFoundException”错误? - Why am I getting a “java.lang.ClassNotFoundException” error after compiling my program? 为什么我的项目中出现“ java.lang.ClassNotFoundException”错误 - why am I getting “java.lang.ClassNotFoundException” error in my project 编译错误:com.google.gson.Gson类型没有源代码 - Compilation error: No source code is available for type com.google.gson.Gson 获取java.lang.ClassNotFoundException,即使我已经使用-cp指定了正确的jar - Getting a java.lang.ClassNotFoundException even though i've specified the correct jar with -cp java.lang.NoClassDefFoundError:com.google.gson.Gson是受限制的类。 有关更多详细信息,请参见Google App Engine开发人员指南。 - java.lang.NoClassDefFoundError: com.google.gson.Gson is a restricted class. Please see the Google App Engine developer's guide for more details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM