Resolving Maven Compilation Error: org.apache.commons.lang Package Does Not Exist (Java Project)

Dec 07, 2025 · Programming · 7 views · 7.8

Keywords: Maven | Java | Dependency Management

Abstract: This article provides an in-depth analysis of the compilation error 'org.apache.commons.lang package does not exist' encountered in Java Struts projects using Maven. By exploring Maven's dependency management mechanisms and referencing best-practice solutions, it offers diagnostic methods using commands like mvn dependency:tree and mvn help:effective-pom, and explains issues such as dependency version conflicts, local repository caching, and POM configuration impacts. The discussion also covers the fundamental differences between HTML tags like <br> and character \n, helping developers understand and resolve similar dependency problems effectively.

Problem Background and Symptoms

In Java Struts project development using MyEclipse IDE, developers often encounter Maven compilation errors, specifically: after importing the org.apache.commons.lang.StringUtils class and adding the JAR file to the build path in the IDE, code intelligence works normally with no errors, but when executing the mvn clean package command, Maven reports "The package org.apache.commons.lang does not exist" error. Despite declaring the dependency in the POM file, for example:

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

and the JAR file existing in the local repository, the issue persists. More confusingly, after deleting the commons-lang folder from the local repository, Maven may download an incorrect version (e.g., 2.1 instead of 2.4) upon re-download, yet compilation still fails.

Core Problem Analysis

This error typically stems from configuration issues or environmental inconsistencies in Maven dependency management. Key points include:

  1. Dependency Version Conflicts: Multiple POM files or parent modules in the project may override dependency versions. For instance, other dependencies might indirectly introduce different versions of commons-lang, causing Maven to select the wrong version during resolution.
  2. Local Repository Cache Issues: Even if the JAR file exists, metadata (e.g., .pom files) might be corrupted or inconsistent, affecting Maven's dependency resolution.
  3. Build Path Configuration: The build path in the IDE may not synchronize with Maven's classpath, leading to unavailable dependencies during compilation.

Solutions and Diagnostic Steps

Based on the best answer, the following diagnostic and resolution steps are recommended:

  1. Run Dependency Tree Analysis: Execute the mvn dependency:tree command to view the hierarchical structure of all dependencies in the project. This helps identify if commons-lang is excluded or overridden by other dependencies. For example, the output might show:
[INFO] +- commons-lang:commons-lang:jar:2.1:compile (version managed from 2.4)

This indicates the version is managed as 2.1, explaining the phenomenon of downloading the wrong version.

<ol start="2">
  • Check Effective POM: Run the mvn help:effective-pom command to generate the final POM after merging all parent POMs and configuration files. Search for commons-lang to confirm the dependency configuration is correct and check for any <exclusions> or <dependencyManagement> overrides.
  • Verify Dependency Copying: Execute the mvn dependency:copy-dependencies command to check if the commons-lang JAR is copied to the target/dependency directory. If missing, it indicates the dependency is not correctly resolved.
  • Supplementary References and Best Practices

    Other answers suggest using the org.apache.commons:commons-lang3 dependency, but note:

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

    Avoid typos or mismatched groupId.

    In-Depth Understanding and Preventive Measures

    To prevent similar issues, developers should:

    Through systematic diagnosis and adherence to Maven best practices, dependency errors can be effectively resolved, enhancing project build stability.

    Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.