获取java.lang.类时未发现异常:maven项目的gson

wbrvyc0a  于 2022-11-06  发布在  Java
关注(0)|答案(5)|浏览(173)

我是新的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

从错误中很明显,它无法Gson jar。
下面是我的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>

我在编译时没有收到任何错误,我的项目部署正确。我使用Jboss 7作为。但在运行时调用actionListener方法时收到此错误。
我已经浏览了stackoverflow中的几个链接,特别是下面的链接。
GSON is not being imported into the maven pproject
但它并没有达到我的目的。有人能提供任何解决方案吗?提前感谢。

edqdpe6u

edqdpe6u1#

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

3gtaxfhh2#

在Gradle中,我添加了以下内容,错误已经消失。我正在尝试构建宁静-BDD项目

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

我的依赖项如下:

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

9nvpjoqh3#

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

mwg9r5ms

mwg9r5ms4#

确保您的项目/应用程序可以使用最新的gson jar文件。如果没有或不确定,请访问https://github.com/google/gson并阅读readme.md文件。下载部分底部的链接是https://maven-badges.herokuapp.com/maven-central/com.google.code.gson/gson。在登录页面上,点击下载(文件下载)链接,以访问最新的jar文件。下载文件,并确保它现在可以被您的项目/应用程序访问。确保在分发的包中正确地包含gson jar文件,并且为客户端正确地安装了该文件。
注意:gson jar文件所需的位置将取决于您的执行环境和您正在使用的开发工具。我目前在Windows上使用Eclipse,其中gson jar文件必须包含在Referenced Libraries下。

vs91vp4v

vs91vp4v5#

已解决Intellij Idea社区版中的问题
1.首先在Pom.xml中为依赖项添加最新相关Gson库

<dependency>
     <groupId>com.google.code.gson</groupId>
     <artifactId>gson</artifactId>
     <version>2.8.2</version>
 </dependency>

1.然后转到intellij项目文件-〉项目结构-〉
1.在左手面板中选择相关的Gson。右侧面板中不应出现以下错误。

1.如果有任何问题,请从this site下载相关的jar文件
以下是针对Gson版本2.8.2的
gson jar files for version 2.8.2
1.然后将相关的jar文件上传到本地库文件夹中。

1.然后执行该高速缓存失效并重新启动

相关问题