java.lang.NoClassDefFoundError:com / google / gson / Gson [英] java.lang.NoClassDefFoundError: com/google/gson/Gson

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

问题描述

我注意到一个奇怪的问题。我可以使用Junit运行我的测试用例,但是当我使用maven运行时测试用例之一是失败的。它是抱怨Gson类def没有找到。



我可以看到Maven依赖关系中的Gson jar。



所以我怀疑classpath不包括Gson。所以我用-X运行maven,并注意到一些线索。

  [DEBUG]找不到元数据com.example.libraries:符号学:1.0.0-SNAPSHOT / maven-metadata.xml在本地(C:\Users\ra\m2\repository)
[DEBUG]跳过远程更新检查com.example.libraries:符号学:1.0.0-SNAPSHOT / maven-metadata.xml,本地缓存的元数据是最新的。
[DEBUG]找不到元数据com.example.libraries:符号学:1.0.0-SNAPSHOT / maven-metadata.xml在本地(C:\Users\ra\m2\repository)
[DEBUG]跳过远程更新检查com.example.libraries:符号学:1.0.0-SNAPSHOT / maven-metadata.xml,本地缓存的元数据是最新的。
[警告] com.example.libraries的POM:符号学:jar:1.0.0-SNAPSHOT无效,传递依赖(如果有)不可用:在构建com的有效模型时遇到2个问题.example.libraries:符号学:1.0.0-SNAPSHOT
[ERROR]'dependencies.dependency.artifactId'for :: jar缺少。 @
[ERROR]'dependencies.dependency.groupId'for :: jar缺少。 @

我有一个依赖于Symbology项目的项目,反过来又使用Gson。
但现在从这个日志我看到传递依赖没有被包含。所以没有找到Gson类。



这里是符号学pom:

 < 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> com.example.libraries< / groupId>
< artifactId>符号学< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< packaging> jar< / packaging>

< name>符号学< / name>
< url> http://maven.apache.org< / url>

< properties>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>

<依赖关系>
<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.10< / version>
< scope> test< / scope>
< / dependency>
<依赖关系>
< groupId> com.google.code.gson< / groupId>
< artifactId> gson< / artifactId>
< version> 2.1< / version>
< / dependency>
<依赖关系>
< groupId> log4j< / groupId>
< artifactId> log4j< / artifactId>
< version> 1.2.16< / version>
< / dependency>
< / dependencies>



这是我的Pom正在调用symbology的项目:

  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> com.example.libraries< / groupId>
< artifactId> FGF< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< packaging> jar< / packaging>
< distributionManagement>
< snapshotRepository>
< id> example.com< / id>
< name> example.com-snapshots< / name>
< url> http:// example / artifactory / libs-snapshots-local< / url>
< / snapshotRepository>
< / distributionManagement>

< name> FGF< / name>
< url> http://maven.apache.org< / url>

< properties>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>

<依赖关系>

<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.10< / version>
< scope> test< / scope>
< / dependency>
<依赖关系>
< groupId> log4j< / groupId>
< artifactId> log4j< / artifactId>
< version> 1.2.16< / version>
< / dependency>
<依赖关系>
< groupId> com.example.libraries< / groupId>
< artifactId>类别< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< / dependency>
<依赖关系>
< groupId> com.example.libraries< / groupId>
< artifactId>时间< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< / dependency>
<依赖关系>
< groupId> com.example.libraries< / groupId>
< artifactId>显示< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< / dependency>
**< dependency>
< groupId> com.example.libraries< / groupId>
< artifactId>符号学< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< / dependency> **
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< version> 3.2.0.BUILD-SNAPSHOT< / version>
< / dependency>
<依赖关系>
< groupId> cglib< / groupId>
< artifactId> cglib< / artifactId>
< version> 2.2.2< / version>
< / dependency>
<依赖关系>
< groupId> org.apache.directory.studio< / groupId>
< artifactId> org.apache.commons.lang< / artifactId>
< version> 2.6< / version>
< / dependency>

< / dependencies>

解决方案


问题是当我将符号系统工件推送到存储库时,它不会将pom推入存储库。所以当我使用符号系统工件作为依赖项时,我的项目无法知道什么是传递依赖关系。



所以我把我的pom和jar一起推入存储库,现在它能够获得所有的依赖。



I noticed a strange problem. I am able to run my test cases using Junit but when I am running using maven One of the test case is failing. It is complaining that Gson class def is not found.

I am able to see the Gson jar in Maven dependencies.

So I doubted that classpath is not including the Gson. So I ran maven with -X and noticed some clues.

[DEBUG] Could not find metadata com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml in local (C:\Users\ra\.m2\repository)
[DEBUG] Skipped remote update check for com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml, locally cached metadata up-to-date.
[DEBUG] Could not find metadata com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml in local (C:\Users\ra\.m2\repository)
[DEBUG] Skipped remote update check for com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml, locally cached metadata up-to-date.
[WARNING] The POM for com.example.libraries:Symbology:jar:1.0.0-SNAPSHOT is invalid, transitive dependencies (if any) will not be available: 2 problems were encountered while building the effective model for com.example.libraries:Symbology:1.0.0-SNAPSHOT
[ERROR] 'dependencies.dependency.artifactId' for ::jar is missing. @ 
[ERROR] 'dependencies.dependency.groupId' for ::jar is missing. @ 

I have a project which depends on Symbology project and this in turn uses Gson. But now from this log I am seeing transitive dependencies are not getting included. So Gson class is not being found.

Here is the Symbology pom:

<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>com.example.libraries</groupId>
<artifactId>Symbology</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Symbology</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
</dependencies>

Here is the Pom of my project which is calling symbology :

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>com.example.libraries</groupId>
<artifactId>FGF</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<distributionManagement>
    <snapshotRepository>
        <id>example.com</id>
        <name>example.com-snapshots</name>
        <url>http://example/artifactory/libs-snapshots-local</url>
    </snapshotRepository>
</distributionManagement>

<name>FGF</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Category</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Time</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Display</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    **<dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Symbology</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>**
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.0.BUILD-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.directory.studio</groupId>
        <artifactId>org.apache.commons.lang</artifactId>
        <version>2.6</version>
    </dependency>       

</dependencies>

解决方案

Figured Out the Problem Finally after spending lot of time.

The problem is when I am pushing the symbology artifact to repository it is not pushing the pom into repository. So when I am using the symbology artifact as dependency my project is not able to know what are the transitive dependencies.

So I pushed my pom into repository along with jar and now it is able to get all the dependencies.

HURRAYYYYYYYYYYYYYYYYY!!!!!!!!!!!!!!!!!!

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

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