Sitemap

Setup Vim + Checkstyle + Java

7 min readNov 14, 2017
Syntastic display syntax errors

As I mentioned in a previous post. I’ve been programming in Java 8 since last september. To be honest, I’m not a big fan of Java but is a good chance for seeing what’s new in the platform and one of the major troubles I had for retaking Java was: choosing an IDE.

At least in Java, choosing an IDE could be hard, Why? Because there are many of them in the market that could help you to do your tasks. Even so, there are three of them which have a huge advantages on this race, and those are: IntelliJ IDEA, Eclipse, and NetBeans.

In the past, I was a huge fan of Eclipse in particular STS (a modified version of Eclipse completely integrated with Spring framework). I remember doing all my work in that IDE (at that time, it was the best). But the time has gone by, and I’ve switched a couple of times language and platforms and now IntelliJ IDEA has become the best IDE in the neighborhood.

So, now that I return to Java, I gave a chance to the best IDE of the neighborhood but, sadly it won’t work for me. Maybe, it was because of the operating system (Ubuntu 14.04.5 LTS), the vim plugin or maybe because I don’t have the money to buy a subscription to the Ultimate edition but, the IDE freezes at least once per hour.

After two days of this, I completely removed the IDE from the OS and I started to thinking why should I use an IDE? I mean, in the past 5 years I haven’t used one, why should I start now? Well, the main reason for using one is: code completion. You know, the thing that happens when you put the name of a class or a variable of an object followed by a dot.

Top 20 Features of Code Completion in IntelliJ IDEA
Top 20 Features of Code Completion in IntelliJ IDEA

Another reasons are: debugging tools, compile and build of the project, resource management and the most important: it’s the standard tool for every developer in the team. But an IDE is just a tool, an option, there is no such thing that says: if you are not using one, you can’t write code, there should be options for replacing it.

The way that I replaced that tool, was with Vim. But, not Vim alone. Programming with only pure and plain Vim should be considered as a suicidal act. Why do I say that? Because, Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. By default, Vim has support for autocompletion (not the one that I mentioned before), supports syntax highlighting, supports hundreds of programming languages and file formats, among other things. That’s why I’m using Vim with a couple of plugins to make my life easier.

Even so, the first thing that I missed, and I have a warning for the team, was: remove unused imports. In Vim there is no such thing like that, and Java doesn’t check it at compilation time like Elixir. Fortunately, a friend (

) reminded me a tool that I used a couple of times at the beginning of my career: checkstyle

Checkstyle

For those who don’t know what it is:

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

Checkstyle is highly configurable and can be made to support almost any coding standard. An example configuration files are supplied supporting the Sun Code Conventions, Google Java Style.

In a nutshell, Checkstyle allows to us to write rules. Rules that should be shared to the whole development team in order to create an standard format coding and allow to new developers to integrate easily to the team coding style.

With this tool we can check:

  • Annotations
  • Block Checks
  • Class Design
  • Coding
  • Headers
  • Imports
  • Javadoc Comments
  • Metrics
  • Modifiers
  • Naming Conventions
  • Regexp
  • Size Violations
  • Whitespace
  • Miscellaneous

The only thing that we need to do is: create a configuration file. A Checkstyle configuration specifies which modules to plug in and apply to Java source files. Modules are structured in a tree whose root is the Checker module. The next level of modules contains:

FileSetChecks - modules that take a set of input files and fire error messages.

Filters - modules that filter audit events, including error messages, for acceptance.

AuditListeners - modules that report accepted events.

The following file is a basic structure of the document

As we said from the begining, everything starts with Checker as the root module. The following line checks that each Java package has a Javadoc file. By default it only allows a package-info.java file, but can be configured to allow a package.html file. The next line is where things get interesting, because TreeWalker checks individual Java source files and defines properties that are applicable to checking such files. The following rules AvoidStarImport, ConstantName and EmptyBlock means that, you should not use imports with the * notation, you should conform constant names to a format, specified by the format property and there should not be empty blocks, respectively.

You can check the whole options for setup in your project in the following url

But configuring this, is a lot of hard work, fortunately there are big companies like google who have his style guide on line, and not only the document in human readable words but also the checkstyle.xml.

So, we are going to download and adapt it to our needs:

The things that I removed were, AvoidStarImport(for now), MemberName, ParameterName, AbbreviationAsWordInName and the javadocs (I’m going to need this but, not for now)

With that in place, now we need a way to display all the code format errors in our project, and guess what? Vim has a plugin for integrate it: vim-syntastic/syntastic

Syntastic

First things first, What is Syntastic? Syntastic is a plugin for Vim created by Martin Grenfell for syntax checking. But it depends on external checkers and displays any resulting errors to the user. You can configure it for using on demand or automatically as files are saved. If syntax errors are detected the user is notified (you don’t need to compile it first). Syntastic has support for many languages like: Go, Elixir, Erlang, Haskell, Java, JavaScript, Vue.js and many more

Now, we need to load Syntastic in our Vim. And for that, we are going to use vim-plug as follows:

Plug 'vim-syntastic/syntastic'

In this post I’m not going to talk about Plugin Managers for vim, if you want to skip it and follow me in this, you can check my vim setup. But, if you want to know more about Plugin Managers, you can search for vundle, neobundle, pathogen or vim-plug

Now, we need to restart our Vim and write :PlugInstall. After that, we should see the next image:

:PlugInstall

By default vim-syntastic has two checkers: javac and checkstyle. With javac you don’t have to config anything but has one problem pointed out in its doc

This checker is not suitable for use with large Java projects. The design of “javac” makes this checker prone to running into various limitations of your shell, Vim, and your Java compiler. You are strongly advised to use something like Eclim (http://eclim.org/) instead of syntastic for projects of any substantial size or complexity.

With that being said, we are going to configure checkstyle. And the first thing we need to do, is download checkstyle–8.4-all.jar. Once it’s finished, we need to enable the checkstyle checker and setup both paths (jar and xml).

To achieve that, we need to put the following lines in our vimrc (If you have download my setup this should be at ~/.vim/custom/config):

With the first line we tell to syntastic to use the checkstyle checker instead of javac and with the following lines we specified the path of the jar and xml file.

This is the file that we are going to use checkstyle.xml

Now we follow the Recommended settings that comes in the README file of the syntastic github and we are done:

Now, when we open a java file (with some errors) of a project we could see the following:

Syntastic display errors

Conclusions

Through this post, we learned about the main IDE’s that are in the market for Java. And we also learned an alternative to those. But, as I said at some point, this setup was, because one of them doesn’t work for me and maybe if I tried with the other two I had not been in the position for searching a replacement. Also, this fits for me because I have been working with Vim for the last 5 years, maybe if it wasn’t because of that I wouldn’t be so confident for doing this.

It’s worth mentioning that checkstyle can be integrated in all of the IDE’s that I’ve already mentioned. That’s because that tool is for code format/style and it is completely independent of the IDE.

So, if you have the need for standardized your Java code or want to increase the skills for all the team, this tool can help you.

But for this post we are done, I hope you find it interesting and helpful. And as usual, If you have any comments or any doubts please let me know.

See you next time. Good luck and have fun!

--

--

Felipe Juárez
Felipe Juárez

Written by Felipe Juárez

Software Developer at MakingDevs, a competitive gamer currently playing SC2 and Clash Royale. I love beer, anime, manga, music my kids and my wife

Responses (1)