[java] 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.

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

This question is related to java eclipse jar classpath

The answer is


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


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.


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.


Do the Quick fix in the Markers tab.

Image Source metamug.com

Reference: 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. 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.


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>


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.

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"


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to jar

Running JAR file on Windows 10 Add jars to a Spark Job - spark-submit Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory Maven Error: Could not find or load main class Where can I download mysql jdbc jar from? Access restriction: The type 'Application' is not API (restriction on required library rt.jar) Create aar file in Android Studio Checking Maven Version Creating runnable JAR with Gradle

Examples related to classpath

spark submit add multiple jars in classpath How to resolve this JNI error when trying to run LWJGL "Hello World"? Intellij Cannot resolve symbol on import Eclipse error "Could not find or load main class" "Could not find or load main class" Error while running java program using cmd prompt Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger getting JRE system library unbound error in build path Run a JAR file from the command line and specify classpath How do I resolve ClassNotFoundException? File inside jar is not visible for spring