33

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

Community
  • 1
  • 1
Fry
  • 4,106
  • 9
  • 38
  • 51

11 Answers11

64

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    I actually have the file there as well, but still no luck. Would it cause a conflict if I referenced my external one and had one in the /WEB-INF/lib folder? – Fry Feb 10 '11 at 19:02
  • 1
    To recap, you did drop `gson-1.6.jar` in `/WEB-INF/lib`? The `json-lib-2.4-jdk15.jar` is not Gson. – BalusC Feb 10 '11 at 19:04
  • Correct, I edited my post to correct that I am using Gson, not json-lib. And yes, the Gson-1.6.jar exists in my /WEB-INF/lib as well as me workspace lib folder where I am referencing it from in the Build path – Fry Feb 10 '11 at 19:08
  • OK and *when* did you get this exception? During running some class with `main()` method as application inside the project? Or when running some JSP/Servlet code? I.e. what code exactly is using Gson and how exactly is it been executed? Note that a duplicate reference in build path is unnecessary. That lib should only exist in *Web App Libraries* which is auto-managed by Eclipse. Remove any manual references. – BalusC Feb 10 '11 at 19:11
  • Ok, updated post at the bottom with a little more info. Ok, if I just have it in the webapps/lib folder, I can reference it from there, but I just tried it without the build path reference and got compilation errors that the import could not be resolved – Fry Feb 10 '11 at 19:22
  • 2
    Ok, I think I know what happened, it's weird and probably a bug, but it seems when I put the jar in my /WEB-INF/lib folder, the project in eclipse didn't update even when I restarted eclipse. After I right-click-updated the project, the library showed up and it compiles now - You are correct that the manual reference is not needed. Thanks for your help an patience. – Fry Feb 10 '11 at 19:38
  • Thanks dude, this was a problem I kept running into often. – kiwicomb123 Jan 24 '17 at 18:32
  • @BalusC why Gson imported though Maven does not work (scope :-compile / provided both) but it worked if manually kept GSON in web-INF/lib folder?? any idea? – nick Apr 10 '18 at 02:11
  • 1
    @nick: because you need it during runtime when it's not provided by the target runtime. – BalusC Apr 10 '18 at 06:37
23

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.
Verbeia
  • 4,400
  • 2
  • 23
  • 44
karn
  • 5,963
  • 3
  • 22
  • 29
  • 1
    Thanks for sharing, this method also worked for an Android project of mine as well! – Kingsolmn Nov 02 '12 at 18:18
  • Thank you so much.. As well, fixed the exact same error I suddenly started getting in my android project after a windows update.. 6 hours wasted until I read this... so pissed and tired. – Todd Painton Jun 14 '13 at 04:59
  • Thanks, I was getting this error in an Android project. Your solution fixed it. – Koderok Nov 16 '14 at 09:51
6

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.

Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • 2
    Note that this section is only useful for external projects or libraries which are not dropped in `/WEB-INF/lib`. – BalusC Feb 10 '11 at 19:05
4

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"

2

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

karthik339
  • 199
  • 1
  • 6
1

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>
Buh Buh
  • 7,443
  • 1
  • 34
  • 61
0

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.

mwdoerner
  • 11
  • 1
  • 4
0

Do the Quick fix in the Markers tab.

Image Source metamug.com

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

Sorter
  • 9,704
  • 6
  • 64
  • 74
0

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.

Jeff Taylor
  • 471
  • 2
  • 4
  • 11
0

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.

Jay
  • 1
0

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.

he41
  • 1