Search code examples
javamavencompilationruntime-errornosuchmethoderror

How to use same lib for compile and run at Eclipse?


I have a maven project that was imported as java project. When I run it, I get

Handling error: NestedServletException, Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.lang.time.DateUtils.addDays(Ljava/util/Date;I)Ljava/util/Date;

Using this answer I got this line:

[Loaded org.apache.commons.lang.time.DateUtils from file:/C:/Users/user/.m2/repository/commons-lang/commons-lang/2.0/commons-lang-2.0.jar]

But at pom.xml it's:

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

Considering I have this libs at Java Build Path:

enter image description here

and

enter image description here

How could I resolve this problem? Where can I assured compare my run vs compile libs?

EDIT

this is the output of mvn dependency:tree -Dverbose -Dincludes=commons-lang:

    [INFO] com.companyName.gestao.projectName:projectName-webapp:war:1.0.0-SNAPSHOT
    [INFO] \- com.companyName.gestao.projectName:projectName-business:jar:1.0.0-SNAPSHOT:compile
    [INFO]    +- com.companyName.commons.utils:companyName-commons-utils:jar:1.14.5:compile
    [INFO]    |  \- softdes:softdes-all:jar:1.6.23:compile
    [INFO]    |     \- commons-lang:commons-lang:jar:2.6:compile (version managed from 2.0)
    [INFO]    \- net.sf.jasperreports:jasperreports:jar:companyName-6.4.0.4:compile
    [INFO]       \- org.codehaus.castor:castor-xml:jar:1.3.3:compile
    [INFO]          +- org.codehaus.castor:castor-core:jar:1.3.3:compile
    [INFO]          |  \- (commons-lang:commons-lang:jar:2.6:compile - version managed from 2.0; omitted for duplicate)
    [INFO]          \- (commons-lang:commons-lang:jar:2.6:compile - version managed from 2.0; omitted for duplicate)

Running the same command at business subproject(at same directory level that webapp project):

[INFO] com.companyName.gestao.projectName:projectName-business:jar:1.0.0-SNAPSHOT
[INFO] +- com.companyName.commons.utils:companyName-commons-utils:jar:1.14.5:compile
[INFO] |  \- softdes:softdes-all:jar:1.6.23:compile
[INFO] |     \- commons-lang:commons-lang:jar:2.0:compile

So webapp depends upon business that depends upon companyName-commons-utils that depends upon softdes-all that depends upon commons-lang version 2. But webapp must use commons-lang version 2.6.

Can I accomplish this by changing dependency order at pom.xml?

SECOND EDIT

After adding <exclusion> to commons-lang at business dependency, I run dependency tree maven as stated above. Now, besides BUILD SUCCESS, maven doesn't have any tree printed, as below:

        PS C:\workspace\projectName-dev\webapp> mvn dependency:tree -Dverbose -Dincludes=commons-lang
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Conciliador - webapp 1.0.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ conciliador-webapp ---
    [WARNING] Using Maven 2 dependency tree to get verbose output, which may be inconsistent with actual Maven 3 resolution
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 8.629s
    [INFO] Finished at: Thu May 17 13:21:08 BRT 2018
    [INFO] Final Memory: 29M/494M
    [INFO] ------------------------------------------------------------------------
    PS C:\workspace\projectName-dev\webapp>

I discovered that commons-lang dependency is inside dependencyManagement as stated here and DateUtils is still loaded from 2.0 version Jar. What could be done?


Solution

  • As @LuisMuñoz stated on comments, I added exclusions to all possible libs using older dependencies. Even with this Eclipse insisted in using older versions of it. I went to project Properties > Java Build Path > Order and Export and top prioritized over everything else my newer dependency version 2.6. After this it worked. Sounds like pom definitions doesn't matter at all if is Eclipse configs that have the final word.