Checkstyle Install Guide PDF
Checkstyle Install Guide PDF
Download Checkstyle
• Checkstyle-6.2.2.jar
• Checkstyle xml file
Run Checkstyle
Checkstyle needs to be run on java source code. So, if you haven’t created or
written your .java files yet, you have to do so first.
Place the checkstyle files in the same directory as the code (.java files) and from
the command-line, execute the following command:
java -jar checkstyle-6.2.2.jar -c cs1331-checkstyle.xml
1
The java -jar part means that I’m executing a .jar file. Then I give it the
checkstyle file. Then I give it the 1331 specific instructions through the .xml file.
Then I tell it what files I want it to check. *.java means all the java files in the
current directory. Make sure that you have already navigated to the directory
where your code is before you run checkstyle.
Here is what it looks like if you have some checkstyle errors (Don’t worry about
what these errors mean right now)
After going through my code and fixing all the errors, I run checkstyle again to
make sure there are no more errors
We take one point off your homework for each checkstyle error, so
make sure you run checkstyle before submitting!
Aliases
That is a very long command and a long process. You’re probably going to have
to open this guide and copy/paste it every time you want to use it. That’s a
little less than ideal isn’t it? And who wants to remember to copy the checkstyle
file into each homework directory? You’re a computer science student - you’re
always looking for ways to do less work!
Well, we can shorten this whole process down. In fact, you can run checkstyle on
any of your code in any directory by simply typing in the following command:
checkstyle *.java
Isn’t that so much easier?
Here’s how to do it.
2
alias checkstyle=‘java -jar /home/yash/cs1331/bin/checkstyle-6.2.2.jar -c
/home/yash/cs1331/bin/cs1331-checkstyle.xml’
remember to replace the directory here with wherever your cs1331 directory is.
5. Save and close the file (Ctrl-X closes the file in nano. Then hit Y when it
asks whether you want to save it, and then hit enter to confirm). Restart
the terminal, and you’re done!
(If it didn’t work, try adding the same line in ~/.bash_profile instead of
~/.bashrc or vice-versa)
Screenshots of the process:
• Open terminal. You can see that my checkstyle alias is not set yet.
• Open ~/.bashrc. Yours will probably look different. Add the checkstyle
alias into the file.
• Save and close the file. Restart terminal. Now checkstyle alias is set. Note:
you still have to tell checkstyle what file to check or it will complain like it
did in the screeshot
3
Windows
There are many ways to access a terminal in windows. The ways to make aliases
with the default command-prompt and with powershell are very complicated
and will not be shown here. (You could Google them and figure out how to do
them if you want).
Aliasing in Windows can be done easily if you use git bash as your terminal.
Here’s how:
1. Open git bash
2. Type the command cd and hit enter. This navigates to your home directory.
3. Type in touch .bashrc (touch will create the file)
4. Type in vim .bashrc (this will let you edit the file)
5. In vim, typing ‘a’ will let you begin editing the file
6. Type in: alias checkstyle=‘java -jar /c/cs1331/bin/checkstyle-6.2.2.jar -c
/c/cs1331/bin/cs1331-checkstyle.xml’
7. Type Esc to stop editing then type :wq and hit Enter to save and quit.
8. Restart git bash and you’re done!
Remember to replace the path in step 6 with the path to where your cs1331
folder is.
If it didn’t let you create the file in step 3, then try the command touch
.bashrc. - Windows will remove the dot at the end by itself.
If it didn’t work, try again with .bash_profile instead of .bashrc
Screenshots of the process:
• Open git bash, navigate to home and create the file (in my case, I had to
use .bash_profile)
4
• Open the file in vim and add the alias.
(screenshot doesn’t show the xml part of the command, but it must be
included)
• Save and close the file. Restart git bash and test whether it worked
Now you know how to run checkstyle and how to make an alias so
that it is easy to use!