获取Maven项目的java.lang.ClassNotFoundException:com.google.gson.Gson [英] Getting java.lang.ClassNotFoundException: com.google.gson.Gson for maven project

查看:725
本文介绍了获取Maven项目的java.lang.ClassNotFoundException:com.google.gson.Gson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 引发:java.lang.NoClassDefFoundError:com / google / gson / Gson 
at com.edfx.tsn.web.controller.DataController.transferData(DataController.java: (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。调用(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。 / p>

下面是我的pom.xml文件

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>
<配置>
< source> 1.7< / source>
< target> 1.7< / target>
< / configuration>
< / plugin>
< / plugins>
< / build>
<依赖关系>
< dependency>
< groupId> com.google.code.gson< / groupId>
< artifactId> gson< / artifactId>
< version> 2.2.4< / version>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> javax.servlet-api< / artifactId>
< version> 3.0.1< / version>
< /依赖关系>
< dependency>
< groupId> com.sun.faces< / groupId>
< artifactId> jsf-api< / artifactId>
< version> 2.2.6< / version>
< /依赖关系>
< dependency>
< groupId> com.sun.faces< / groupId>
< artifactId> jsf-impl< / artifactId>
< version> 2.2.6< / version>
< /依赖关系>
< /依赖关系>
<包装>战争< / packaging>
< / project>

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

我已经通过了stackoverflow中的几个链接,特别是下面的



GSON是没有被导入到maven pproject中



但它不符合我的目的。任何人都可以提供任何解决方案。提前感谢。

解决方案

  1。去你的回购地点,并检查罐子是否正确下载。 
2.解开你的WAR并检查它是否真的可用。
3. althoug你的maven依赖看起来很好,但是试试这个:
< dependency>
< groupId> com.google.code.gson< / groupId>
< artifactId> gson< / artifactId>
< version> 2.3< / version>
< /依赖关系>
4.确保您执行mvn clean:从项目的命令提示符位置安装-P。
5.我尝试了相同的依赖关系,并且它对JBOSS也能正常工作。


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.

解决方案

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.

这篇关于获取Maven项目的java.lang.ClassNotFoundException:com.google.gson.Gson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆