我是maven的新手,我正在使用apache-maven-3.2.2来构建我的项目。它是一个简单的项目,将从客户端接收json数据,然后在服务器端将其转换为类似的java类。 json转换为Java格式,我们使用的是Google的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>
我在编译时没有收到任何错误,并且我的项目已正确部署。我使用的是Jboss7,但是当我的actionListener方法被调用时,却在运行时出现了此错误。
我已经通过了stackoverflow中的几个链接,特别是下面的一个
但这并没有达到我的目的。任何人都可以对此提供任何解决方案。谢谢。
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.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句