Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am new to maven.I am using apache-maven-3.2.2 for building my project.Its simple project which will received json data from client side and on server side it will convert this json data to its analogous java class.For conversion of json to java format we are using google's Gson library.Without maven my project is running properly.but when I converted it to maven then I got the following error:

   Caused by: java.lang.NoClassDefFoundError: com/google/gson/Gson
   at com.edfx.tsn.web.controller.DataController.transferData(DataController.java:51) [classes:]
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_17]
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_17]
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_17]
   at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_17]
   at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.13.Final.jar:]
   at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.13.Final.jar:]
   at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.7-jbossorg-2.jar:]
   at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
   ... 22 more

From the error its pretty clear that its unable to Gson jar.

Now below is my pom.xml file

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>TestJSON</groupId>
   <artifactId>TestJSON</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>TestJSON</name>
  <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.6</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.6</version>
    </dependency>
  </dependencies>
   <packaging>war</packaging>
  </project>

I am not getting any error at compile time and my project is getting deployed properly.I am using Jboss7 as.But I am getting this error at runtime when my actionListener method is getting invoked.

I have already gone through couple of links in stackoverflow specially the below one

GSON is not being imported into the maven pproject

but it didn't serve my purpose.Can anyone provide any solution to this.Thanks in advance.

I did a quick test with your same Gson dependency and it works for me. I'm on Tomcat 7 though, I don't know if there could be any problem with JBoss modules. I this it's a runtime problem, since NoClassDefFoundError is thrown when the searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found. Is that your whole POM? Did you do a mvn clean, just in case? How do you deploy your war in JBoss? – watery Aug 31, 2014 at 9:11 yes I have done a maven build and got build success.I followed the normal deployment procedure in JBoss7.By adding the project to the server my project got deployed. – Sumit Ghosh Aug 31, 2014 at 16:15 I don't have any other idea: did the gson jar get copied to the war WEB-INF/lib directory? – watery Aug 31, 2014 at 19:46 Perhaps something is really poorly configured. Try commenting out the gson entry in the pom and see if the compile still works. If it doesn't that is a good sign. If it does then you should look at how you have laid out your project structure – user2122031 Sep 1, 2014 at 1:49
1. go to your repo location and check if the jar is properly downloaded.
2. unpack your WAR and check if the jar is actually available in it.
3. althoug your maven dependency looks fine, but just try using this: 
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3</version>
    </dependency>
4. Make sure you do a mvn clean:install -P from command prompt location of the project.
5. I tried with the same dependency and it works fine with JBOSS too.
                I have followed your steps.First I went to my repo location and I have checked that gson jar has been downloaded.Then I open my war with 7 zip and found that inside lib folder there exist jsf-api and jsf-impl jar but no gson jar was available there.then From coomand prompt I have done mvn clean install.I got build success result then when I ran my application it continues to give the error :"java.lang.NoClassDefFoundError: com/google/gson/Gson ".I surprised to see although gson has been downloaded in repo location then during deployment time why it is not available inside the war lib folder?
– Sumit Ghosh
                Sep 2, 2014 at 18:06
                i am able execute mvn clean install but mvn clean:install -P is not executing, instead i am getting error Could not find goal 'install' in plugin org.apache.maven.plugins:maven-clean-plugin:2.5 .
– Amol Bais
                Dec 31, 2018 at 11:02

In Gradle I have added following and error has gone. I am trying to build serenity-BDD project

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'

My dependencies are following:

   dependencies {
    def SERENITY_VERSION = "1.6.7-rc.1"
    compile 'net.serenity-bdd:serenity-core:' + SERENITY_VERSION
    compile 'net.serenity-bdd:serenity-rest-assured:' + SERENITY_VERSION
       compile group: 'net.serenity-bdd', name: 'serenity-cucumber', version: '1.1.36'
    compile group: 'info.cukes', name: 'cucumber-core', version: '1.2.5'
    testCompile 'org.assertj:assertj-core:3.1.0'
    testCompile 'com.googlecode.lambdaj:lambdaj:2.3.3'
    testCompile 'io.codearte.jfairy:jfairy:0.5.3'
    compile 'net.serenity-bdd:serenity-screenplay:' + SERENITY_VERSION
    compile 'net.serenity-bdd:serenity-screenplay-webdriver:' + SERENITY_VERSION
    compile 'net.serenity-bdd:browse-the-web:1.1.34'
    testCompile 'net.serenity-bdd:serenity-junit:' + SERENITY_VERSION
   '3.5.3'
    testCompile 'org.slf4j:slf4j-simple:1.7.12'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'}
                Now-a-days it needs to be (instead of compile group):   implementation "com.google.code.gson:gson:2.7"
– Mark
                Apr 1, 2019 at 2:45

Insure that the latest gson jar file is available to your project/application. If not or unsure, go to https://github.com/google/gson and read the readme.md file. The link at the bottom of the Download section is https://maven-badges.herokuapp.com/maven-central/com.google.code.gson/gson On the landing page, click on the Download (File Download) link in the upper right to access the latest jar files. Downloaded file and insure that it's now accessible by your project/application. Make sure to correctly include the gson jar file in your distributed package and that it's correctly installed for your clients.

Note: The needed location for the gson jar file will depend on your execution environment and the development tools that you are using. I'm currently using Eclipse on Windows where the gson jar file must be included under Referenced Libraries.

Solved the issue in Intellij Idea Community Edition

  • First add latest relevant Gson library for dependencies in Pom.xml

     <dependency>
         <groupId>com.google.code.gson</groupId>
         <artifactId>gson</artifactId>
         <version>2.8.2</version>
     </dependency>
    
  • Then go to the intellij project File -> Project Structure -> Libreries

  • Select the relevant Gson in left hand panel. There shouldn't be errors as below in right side panel.

  • If there any issue, download relevant jar files from this site
  • below is for Gson version 2.8.2

    gson jar files for version 2.8.2

  • Then upload relevant jar files to local library folder. In my case I have to upload source.jar and javadoc.jar
  • then do the Invalidate the cache and restart
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •