Apache Commons is a popular set of reusable Java components developed by the Apache Software Foundation. Its goal is to provide well-tested, reusable code to simplify common programming tasks, so you don’t have to reinvent the wheel. It is widely used in enterprise and open-source projects.
Here’s a structured overview:
1. Core Libraries in Apache Commons
Apache Commons is divided into multiple sub-projects (modules), each focusing on a specific area. Some of the most commonly used are:
Module | Purpose |
---|---|
Commons Lang | Utilities for java.lang classes (String manipulation, ObjectUtils , ArrayUtils , NumberUtils ) |
Commons IO | Utilities for I/O operations (reading/writing files, file filters, file comparison) |
Commons Collections | Enhancements to Java collections (e.g., Bag , MultiMap , BidiMap ) |
Commons Math | Mathematical and statistical functions |
Commons Codec | Encoding and decoding utilities (Base64, Hex, URL encoding) |
Commons BeanUtils | Reflection and property manipulation for JavaBeans |
Commons Configuration | Handling configuration files (properties, XML, INI) |
Commons Validator | Validation framework for strings, emails, dates, etc. |
2. Adding Apache Commons to a Project
If you’re using Maven, you can add a dependency like this:
<!-- Example: Apache Commons Lang -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>
For Gradle:
implementation 'org.apache.commons:commons-lang3:3.13.0'
3. Example Usage
Commons Lang (StringUtils)
import org.apache.commons.lang3.StringUtils;
public class Example {
public static void main(String[] args) {
String str = " Hello World ";
System.out.println(StringUtils.isBlank(str)); // false
System.out.println(StringUtils.trim(str)); // "Hello World"
System.out.println(StringUtils.reverse(str)); // " dlroW olleH "
}
}
Commons IO (FileUtils)
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class ExampleIO {
public static void main(String[] args) throws IOException {
File file = new File("example.txt");
// Write string to file
FileUtils.writeStringToFile(file, "Hello Apache Commons!", "UTF-8");
// Read string from file
String content = FileUtils.readFileToString(file, "UTF-8");
System.out.println(content);
}
}
4. Why Use Apache Commons?
- Reduces boilerplate code – simplifies operations on Strings, collections, files, etc.
- Well-tested and maintained – reduces bugs compared to writing your own utility methods.
- Cross-project consistency – widely adopted across Java projects.
1. Commons Lang (org.apache.commons.lang3)
String Utilities – StringUtils
import org.apache.commons.lang3.StringUtils;
String str = " Hello World ";
// Check if string is empty or blank
StringUtils.isEmpty(str); // false
StringUtils.isBlank(str); // false
// Trim, reverse, capitalize
StringUtils.trim(str); // "Hello World"
StringUtils.reverse(str); // " dlroW olleH "
StringUtils.capitalize("hello"); // "Hello"
// Check prefix/suffix ignoring case
StringUtils.startsWithIgnoreCase("Hello", "he"); // true
Object Utilities – ObjectUtils
import org.apache.commons.lang3.ObjectUtils;
Object a = null;
Object b = ObjectUtils.defaultIfNull(a, "Default"); // "Default"
Array Utilities – ArrayUtils
import org.apache.commons.lang3.ArrayUtils;
int[] numbers = {1, 2, 3};
ArrayUtils.add(numbers, 4); // [1, 2, 3, 4]
ArrayUtils.contains(numbers, 2); // true
2. Commons IO (org.apache.commons.io)
File Utilities – FileUtils
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
File file = new File("example.txt");
// Write to file
FileUtils.writeStringToFile(file, "Hello World!", "UTF-8");
// Read from file
String content = FileUtils.readFileToString(file, "UTF-8");
// Copy files
FileUtils.copyFile(new File("source.txt"), new File("dest.txt"));
Filename and File Filters
import org.apache.commons.io.FilenameUtils;
String name = "example.txt";
FilenameUtils.getExtension(name); // "txt"
FilenameUtils.getBaseName(name); // "example"
3. Commons Collections (org.apache.commons.collections4)
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;
Bag<String> bag = new HashBag<>();
bag.add("apple", 2); // adds 2 apples
bag.add("orange");
bag.getCount("apple"); // 2
4. Commons Codec (org.apache.commons.codec)
import org.apache.commons.codec.binary.Base64;
String original = "Hello";
String encoded = Base64.encodeBase64String(original.getBytes());
String decoded = new String(Base64.decodeBase64(encoded));
5. Commons Math (org.apache.commons.math3)
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
DescriptiveStatistics stats = new DescriptiveStatistics();
stats.addValue(10);
stats.addValue(20);
stats.addValue(30);
stats.getMean(); // 20.0
stats.getStandardDeviation(); // ~8.16
6. Commons BeanUtils (org.apache.commons.beanutils)
import org.apache.commons.beanutils.BeanUtils;
class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Person p = new Person();
BeanUtils.setProperty(p, "name", "Alice");
String name = BeanUtils.getProperty(p, "name"); // "Alice"