I’m looking for a java lint tool that can run on the command line, independent of Eclipse, to be used as a git commit hook or in an automated build script. It should check for unused imports, variables, style guidelines, and proper exception handling. Does such a tool exist, and what are the best options?
Hey @mehta_tvara If you’re primarily looking for a tool to enforce style guidelines, Checkstyle is a great option. It’s a widely used CLI tool that ensures code adheres to standard conventions like Google’s Java Style Guide or Sun’s Java Coding Standards.
Install Checkstyle:
brew install checkstyle
# macOS
sudo apt install checkstyle
# Debian/Ubuntu
Or download the JAR from Checkstyle’s website.
Run Checkstyle on your Java files:
checkstyle -c /google_checks.xml MyFile.java
Pros: Highly configurable, integrates well with build tools like Maven and Gradle.
Cons: Primarily focused on style issues, not deep static analysis.
If you need something beyond just style enforcement, SpotBugs is a powerful CLI tool for detecting actual bugs like null dereferences, infinite loops, and concurrency issues.
Download the latest release from SpotBugs GitHub.
Extract and run it against your compiled .class files:
spotbugs -textui -effort:max -low MyProject/build/classes
Pros: Finds real bugs, not just style issues.
Cons: Requires compiled class files instead of raw source files.
For a mix of style enforcement and static analysis, PMD is another excellent CLI-based tool. It detects unused variables, unnecessary object creation, improper exception handling, and more.
Install PMD via Homebrew or download from PMD’s website:
brew install pmd
Run PMD on your Java files:
pmd check -d src -R rulesets/java/quickstart.xml -f text
Pros: Covers both code smells and style issues, highly configurable.
Cons: Some rules can be overly strict.