获取 java.lang.ClassNotFoundException: com.google.gson.Gson for maven 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25590303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:17:51  来源:igfitidea点击:

Getting java.lang.ClassNotFoundException: com.google.gson.Gson for maven project

javajsonmavengson

提问by Sumit Ghosh

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:

我是 maven 新手。我正在使用 apache-maven-3.2.2 来构建我的项目。它的简单项目将从客户端和服务器端接收 json 数据,它将将此 json 数据转换为其类似的 java 类。用于转换从 json 到 java 格式,我们使用谷歌的 Gson 库。没有 maven,我的项目运行正常。但是当我将它转换为 maven 时,出现以下错误:

   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.

从错误中很明显它无法 Gson jar。

Now below is my pom.xml file

现在下面是我的 pom.xml 文件

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

我在编译时没有收到任何错误,我的项目正在正确部署。我正在使用 Jboss7 as。但是当我的 actionListener 方法被调用时,我在运行时收到此错误。

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

我已经浏览过 stackoverflow 中的几个链接,特别是下面的一个

GSON is not being imported into the maven pproject

GSON 没有被导入到 maven pproject

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

但它没有达到我的目的。任何人都可以为此提供任何解决方案。提前致谢。

采纳答案by vikesh pandey

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.

回答by dimyo

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

在 Gradle 中,我添加了以下内容并且错误消失了。我正在尝试构建 serenity-BDD 项目

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'}

回答by Kaji Islam

Add below dependency on main pom.xml then do clean build and test <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>1.7.1</version></dependency>

在主 pom.xml 上添加以下依赖项,然后进行清理构建和测试 <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>1.7.1</version></dependency>