Stop fixing Java code style issues manually! This toolkit automatically checks, fixes, and formats your Java code in a single command using Checkstyle and Google Java Format integration.
Quick Start β’ Features β’ Installation β’ Documentation β’ Contributing
- β‘ Save Hours: Automatically fixes 60%+ of common Checkstyle violations
- π― One Command: Replaces complex tool chains with single
checkstylecommand - π§ Zero Config: Works out-of-the-box with sensible defaults
- π Production Ready: Used by students and professionals for code quality assurance
- π Well Documented: Comprehensive guides, examples, and troubleshooting
- π¨ Google Style: Industry-standard formatting built-in
When you run checkstyle YourFile.java, it automatically fixes:
- β
Uppercase 'L' suffix on long literals (
123lβ123L) - β
Array bracket positioning (
String arr[]βString[] arr) - β
Variable naming (
my_varβmyVar, preserves CONSTANTS) - β
Unused star imports (removes unused
import java.util.*;) - β All whitespace/indentation/spacing (via google-java-format)
These violations are detected but require manual fixing:
- β Star imports that ARE being used
- β Multiple variable declarations (
int a, b;β separate lines) - β Missing braces on if/for/while statements
- β Empty catch blocks
- β Magic numbers (hardcoded numeric values)
- β Missing Javadoc comments
# Before: messy code with violations
$ cat MyClass.java
public class my_class{
private long num=123l;
private String arr[]=new String[5];
}
# After: one command fixes everything!
$ checkstyle MyClass.java
β Fixed: Variable naming (my_class β myClass)
β Fixed: Long literal suffix (123l β 123L)
β Fixed: Array bracket position (String arr[] β String[])
β Formatted: Applied Google Java Style
All checks passed! β
# Your code is now clean and compliant
$ cat MyClass.java
public class MyClass {
private long num = 123L;
private String[] arr = new String[5];
}cd scripts
chmod +x setup-checkstyle.sh
./setup-checkstyle.shThen restart your terminal:
source ~/.zshrc # or source ~/.bashrc# Check and auto-fix a single file
checkstyle MyClass.java
# Check and auto-fix all Java files
checkstyle *.java
# Check with custom config
checkstyle -c checkstyle.xml MyClass.java
# Skip auto-formatting (still auto-fixes)
checkstyle --no-format MyClass.java
# Skip auto-fixing (still formats)
checkstyle --no-fix MyClass.java
# Check only (no fixes or formatting)
checkstyle --no-fix --no-format MyClass.java- Java JDK: 8 or higher
- Package Manager: Homebrew (macOS) or apt-get (Linux)
- Optional: VS Code with Java extensions
- Checkstyle: 10.12.7+ (code quality checker)
- google-java-format: Latest (code formatter)
- Python 3: For VS Code settings management (usually pre-installed)
The setup script installs and configures:
- Checkstyle via Homebrew/apt-get
- google-java-format for automatic code formatting
- Global configuration at
~/.config/checkstyle/checkstyle.xml - Wrapper script at
~/.local/bin/checkstyle-wrapper - VS Code integration (if VS Code is installed)
- Shell alias for seamless command-line usage
~/.config/checkstyle/
βββ checkstyle.xml # Global checkstyle configuration
βββ checkstyle-10.12.7-all.jar # Backup JAR file
~/.local/bin/
βββ checkstyle-wrapper # Intelligent wrapper script
~/.checkstyle-google.xml # Google Style configuration
~/Library/Application Support/Code/User/settings.json # VS Code settings (macOS)
~/.config/Code/User/settings.json # VS Code settings (Linux)
~/.zshrc or ~/.bashrc # Shell alias added here
Copy the included checkstyle.xml to your project root and customize:
cp checkstyle.xml /path/to/your/project/Then use it:
checkstyle -c checkstyle.xml *.javaEdit checkstyle.xml to adjust strictness levels:
<!-- Example: Increase line length limit -->
<module name="LineLength">
<property name="max" value="120"/> <!-- Changed from 100 -->
</module>
<!-- Example: Disable a specific check -->
<module name="MagicNumber">
<property name="severity" value="ignore"/>
</module>
<!-- Example: Allow more abbreviations in names -->
<module name="AbbreviationAsWordInName">
<property name="allowedAbbreviationLength" value="2"/> <!-- Changed from 0 -->
</module>The default configuration uses 4-space indentation. To change:
<module name="Indentation">
<property name="basicOffset" value="2"/> <!-- Changed from 4 -->
<property name="braceAdjustment" value="0"/>
<property name="caseIndent" value="2"/> <!-- Changed from 4 -->
</module>-
Pre-processing fixes (Perl regex)
- Fixes uppercase L on long literals
- Repositions array brackets
- Converts snake_case to camelCase for variables
- Removes obviously unused imports
-
Auto-formatting (google-java-format)
- Applies Google Java Style formatting
- Fixes all whitespace and indentation
- Handles line wrapping and spacing
-
Style checking (Checkstyle)
- Validates against remaining rules
- Reports issues that need manual fixing
- Exits with code 0 if all checks pass
The checkstyle-wrapper script:
- Detects available tools (checkstyle, google-java-format)
- Applies fixes before checking
- Supports custom configurations
- Provides helpful options (
--no-fix,--no-format) - Falls back gracefully if tools are missing
After installation, VS Code automatically:
- β Runs Checkstyle on file save
- β Shows violations in Problems panel (Cmd/Ctrl+Shift+M)
- β Highlights issues inline with squiggly lines
- β Provides quick-fix suggestions where possible
Right-click any Java file β "Check Code with Checkstyle"
Edit VS Code settings (settings.json):
{
"java.checkstyle.autocheck": false
}Before (BadStyle.java):
public class BadStyle {
private String user_name;
private long timestamp = 123l;
private String items[] = {"one", "two"};
public void printInfo(){
if(user_name!=null)
System.out.println(user_name);
}
}After running checkstyle BadStyle.java:
public class BadStyle {
private String userName; // Fixed: snake_case β camelCase
private long timestamp = 123L; // Fixed: l β L
private String[] items = {"one", "two"}; // Fixed: array brackets
public void printInfo() { // Fixed: spacing
if (userName != null) { // Fixed: spacing, BUT still missing braces (manual)
System.out.println(userName);
}
}
}Remaining Issues (manual fix required):
[WARN] BadStyle.java:8: 'if' construct must use '{}'s. [NeedBraces]
[WARN] BadStyle.java:1: Missing a Javadoc comment. [MissingJavadocType]
See the examples/ directory for sample files demonstrating:
CommonIssues.java- Various style violations and their fixesBeforeAfter.java- Side-by-side comparisonsManualFixes.java- Issues requiring manual intervention
# Verify installation
which checkstyle
# Should output: checkstyle: aliased to checkstyle-wrapper
# If not, reload shell config
source ~/.zshrc # or ~/.bashrcchmod +x ~/.local/bin/checkstyle-wrapper
chmod +x scripts/setup-checkstyle.sh# Verify installation
which google-java-format
# Reinstall if needed (macOS)
brew install google-java-format
# Or use --no-format flag
checkstyle --no-format MyClass.java- Open VS Code Settings (Cmd/Ctrl+,)
- Search for "checkstyle"
- Verify "Java > Checkstyle: Configuration" points to correct file
- Reload VS Code window
Start with relaxed rules, then gradually increase strictness:
# Use Google config (more lenient)
checkstyle -c ~/.checkstyle-google.xml MyClass.java
# Or customize checkstyle.xml to disable strict checks- Run
checkstyle *.javabefore every submission - Fix auto-fixable issues first, then manual ones
- Use
--no-fix --no-formatto see what checkstyle will find on submission - Keep a backup before first run:
cp MyClass.java MyClass.java.backup
- Integrate into your build process
- Use project-specific
checkstyle.xmlfor team consistency - Run checkstyle in CI/CD pipelines
- Configure IDE to match checkstyle rules
- Commit
checkstyle.xmlto version control - Enforce checks in pull request reviews
- Document project-specific customizations
- Run checkstyle in pre-commit hooks
# Remove installed tools (macOS)
brew uninstall checkstyle google-java-format
# Remove configuration files
rm -rf ~/.config/checkstyle
rm ~/.checkstyle-google.xml
rm ~/.local/bin/checkstyle-wrapper
# Remove shell alias (edit ~/.zshrc or ~/.bashrc)
# Delete lines containing: alias checkstyle='checkstyle-wrapper'If you find this project helpful, please consider giving it a star β
We welcome contributions! This project is continuously improved by the community.
- π Found a bug? Open an issue
- π‘ Have an idea? Request a feature
- π§ Want to contribute? Read our Contributing Guide
- π Security concern? See our Security Policy
Thanks to all who have contributed to making this project better!
- Language: Shell Script, Perl, Java Configuration
- License: MIT (free for personal and commercial use)
- Platforms: macOS, Linux
- Dependencies: Checkstyle, google-java-format, Java 8+
- Status: Actively maintained β
- Windows support (WSL compatibility)
- GitHub Actions integration examples
- Maven/Gradle plugin versions
- Pre-commit hook templates
- Docker container version
- IntelliJ IDEA plugin integration
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License - Copyright (c) 2026 Keith Bishop
Free to use, modify, and distribute with attribution.
Built with:
- Checkstyle by the Checkstyle team - Static code analysis
- google-java-format by Google - Code formatter
- Google Java Style Guide - Industry standard
- Checkstyle community for extensive documentation
- Google for open-source formatting tools
- All contributors and users who provide feedback
- π Check the Troubleshooting section
- π Review examples/ for sample code
- π Consult Checkstyle documentation
- π Open an issue if you're stuck
- β Star this repo to follow updates
- π Watch for new releases
- π Enable notifications for important changes