98

I've been trying to find a Java linter capable of running on the CLI that is separate from Eclipse that I can call as a commit hook for git or from our automated build script. Does such a thing exist?

Ideally it needs to check for unused imports and variables, that style guidelines are followed, exceptions are used properly, etc. Though some subset of those features would be better that what we have now - nothing!

BlueDragonX
  • 983
  • 1
  • 7
  • 4
  • 2
    // , Isn't this a request for a software recommendation? – Nathan Basanese Jun 24 '16 at 21:07
  • 8
    I don't know if I should ask here or on meta, but what's wrong with questions like this that ask for recommendations? Sure there's no single, objective answer to this question but then again that's the case for many technical questions as well. This question could be re-phrased as "I don't have a a linter and I don't know which tools are available, current, and widely used". Would that help? I would much rather find a curated, ranked, and date-logged Q/A on StackOverflow than somewhere else. Where should people with this sort of question go, if not here? – Kyle Krull Mar 18 '19 at 14:09

3 Answers3

95
  • SpotBugs (earlier Findbugs) for finding existing bugs. VERY GOOD!
  • PMD for finding patterns that can lead to bugs (e.g. unused variables)
  • Checkstyle to enforce coding standards and conventions (e.g. whitespace, Javadoc)
  • Error Prone hooks right into your application's compile step
  • clang-format supports java and may be available on your system already

All these tools have some overlapping rules. There are many other similar tools, but these are the most popular and supported.

moo
  • 1,597
  • 1
  • 14
  • 29
mikeslattery
  • 4,039
  • 1
  • 19
  • 14
  • 1
    Would you say that Findbugs and PMD do different things? Last time i looked into this, i concluded they were similar enough that we only needed one (and picked PMD). We also ran Checkstyle, and yes, found that even PMD and Checkstyle overlap. – Tom Anderson Feb 04 '13 at 17:36
  • @TomAnderson - i would say they definitely do different things, yes. there is value to both of them (obviously there is overlap as well). – jtahlborn Feb 04 '13 at 17:37
  • Yes, very different goals. Findbugs tries to find ACTUAL bugs, sometimes by doing deep analysis. PMD just looks for patterns known to sometimes lead to bugs. Personally, I use FindBugs and Checkstyle as there is less overlap. – mikeslattery Feb 04 '13 at 17:38
  • you might want to mention that pmd has 2 different types of checks, pmd and cpd. – jtahlborn Feb 04 '13 at 17:39
  • Thanks for the direction mikeslattery, that helps out a lot! – BlueDragonX Feb 06 '13 at 23:02
  • 4
    Findbugs is now [SpotBugs](https://spotbugs.github.io/) (on [GitHub](https://github.com/spotbugs/spotbugs/)) – Matruskan May 03 '18 at 20:55
12

Also, check out Sonar, which combines many of the other tools and provides a nice view for it too.

rules from Checkstyle, FindBugs, PMD, Clirr, fb-contrib.


Not sure exactly how to add it to a post-commit hook, but http://docs.codehaus.org/display/SONAR/Analyzing+with+Maven might be a good starting point (especially if you're using maven).

Maybe even consider using one of the approaches listed in http://docs.codehaus.org/display/SONAR/Continuous+Integration since it seems that you might be trying to look for better tooling for your whole team ("Though some subset of those features would be better that what we have now - nothing!"

yegeniy
  • 1,272
  • 13
  • 28
  • 2
    +1 Four years after the question was asked, SonarQube seems to be more active, modern and complete. It used to combine the tools mentioned above, but now it implements them natively avoiding the overlaps https://blog.sonarsource.com/already-158-checkstyle-and-pmd-rules-deprecated-by-sonarqube-java-rules/ – user2518618 Jan 17 '18 at 16:57
  • 1
    Still no nice CLI tool for Sonar... – Fabich Feb 10 '21 at 14:42
-3

This is EXACTLY what I am working on: a tool CLI-friendly to be used to check the quality of Java code. It has also an interactive modality. You can run single queries (to check for single warnings) or all queries together.

The tools is in its early stage but I am working on it almost every day. It is available here:

https://github.com/ftomassetti/effectivejava

Please let me know what do you think about it and feel free to ask questions.

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26