2

I've create a Java application, it will use GSON class, so I add gson.jar to the ClassPath of my project, also, I include this line

import com.google.gson.Gson

at the top of my program.

Everything seems to be good, however, once my app runs to this line,

Gson gson = new Gson();

it will pop up errors and say :

org.jboss.resteasy.spi.UnhandledException: java.lang.NoClassDefFoundError: com/google/gson/Gson
...
Caused by: java.lang.NoClassDefFoundError: com/google/gson/Gson

I've search dozens of articles, and I've tried to add new variable to my library folder, as the answer which gave by Vaishali Kulkarni in this article mentioned, but it still doesn't work.

Did I miss anything necessary to use Gson object?

Community
  • 1
  • 1
Alanight
  • 353
  • 2
  • 8
  • 23
  • "once my app runs" - How do you run your program? From an IDE? Which one? – JimmyB Feb 15 '16 at 10:11
  • Yes, I run my program from Eclipse directly. – Alanight Feb 15 '16 at 10:12
  • How are you building your application? This error means that while the library was available at compile time (you were able to compile are run the program) it was not available at runtime. – ewanc Feb 15 '16 at 10:16
  • Maybe some of these answers can help you: http://stackoverflow.com/questions/35305135/noclassdeffounderror-during-runtime-when-using-gson. If not, please introduce more details to your question. – eliocapelati Feb 15 '16 at 10:17
  • GSON has no dependency on org.jboss.resteasy.spi so it's obviuosly not a problem related to the GSON library. – Leo Feb 15 '16 at 10:21
  • 1
    @ewanc I'm using Eclipse Luna, I just use _Debug_ to test it, and my program runs on Tomcat 7.0. Did I answer your question? I'm new to Java & Eclipse, so if I misunderstand you, please tell me. – Alanight Feb 15 '16 at 10:38
  • @eliocapelati Thanks! I'm trying to search any useful message or answer from that. – Alanight Feb 15 '16 at 10:39
  • @Leo OK, but my program run correctly without that line... That's why I suspect that's the reason. – Alanight Feb 15 '16 at 10:40
  • Possible duplicate of [I am getting "java.lang.ClassNotFoundException: com.google.gson.Gson" error even though it is defined in my classpath](https://stackoverflow.com/questions/4961336/i-am-getting-java-lang-classnotfoundexception-com-google-gson-gson-error-even) – ישו אוהב אותך Nov 11 '17 at 17:14
  • Try the accepted solution in [this question](https://stackoverflow.com/questions/4961336/i-am-getting-java-lang-classnotfoundexception-com-google-gson-gson-error-even). Remember follow the comments on the answer too, I think there might be some details in there. It seems this is a very common problem, but there are different answers for different people due to differences in what they have done to solve it and the type of project or build tools. This one seems to match your situation. – ewanc Feb 15 '16 at 10:50
  • That answer is correct! I remove the reference from ClassPath, and put the .jar file in WEB-INFO folder and "right-click -> refresh" the project, then my program could compile & run as I expected. Really appreciate! – Alanight Feb 16 '16 at 02:34

1 Answers1

1

Resolve this by removing the gson test range from the parent pom.

<dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
</dependency>
byuc
  • 11
  • 3