Skip to content

Setting Up CheckStyle

Sean Low edited this page Jun 12, 2021 · 1 revision

Setting Up Checkstyle


  1. Go to your Ubuntu or Mac Terminal and download the checkstyle jar file and the cs2030 xml file to prepare the setup for checkstyle. The following 2 commands below downloads the required files
wget http://www.comp.nus.edu.sg/~cs2030/checkstyle-8.2-all.jar
wget http://www.comp.nus.edu.sg/~cs2030/cs2030_checks.xml
  1. Make sure you place these 2 files in the same directory as your java files.

  2. To run the checkstyle command, execute the following command:

java -jar ./checkstyle-8.2-all.jar -c ./cs2030_checks.xml *.java 
  1. To run the checkstyle on specific files, execute the following command:
java -jar ./checkstyle-8.2-all.jar -c ./cs2030_checks.xml [YOUR_JAVA_FILES]

For example:

java -jar ./checkstyle-8.2-all.jar -c ./cs2030_checks.xml Point.java Circle.java Square.java 

Shortcut to run Checkstyle (Windows and Mac)


All credits from this thread and its contributors - Setting alias for checkstyle #37

Simply by creating aliases!

  1. Create a folder to store checkstyle-8.2-all.jar and cs2030_checks.xml. Refer to the above for more info.

  2. Navigate to the folder that you created with the 2 items inside and enter pwd to print the current directory to fit into PATH_TO_FOLDER.

  3. Edit your terminal setting file.

Windows/WSL

Place the alias at the bottom of your .bashrc file or wherever you prefer. (If you are using zsh, replace .bashrc below with .zshrc.)

1. Edit .bashrc file in vim
vim ~/.bashrc

2a. To make checkstyle alias on all java files in the folder
Add alias to anywhere in the file and save it (w!)
alias checkstyle="java -jar /[PATH_TO_FOLDER]/checkstyle-8.2-all.jar -c /[PATH_TO_FOLDER]/cs2030_checks.xml *.java"

ALTERNATIVE 2b. To make checkstyle alias on specific java file only
Add alias to anywhere in the file and save it (w!)
alias checkstyle="java -jar /[PATH_TO_FOLDER]/checkstyle-8.2-all.jar -c /[PATH_TO_FOLDER]/cs2030_checks.xml"

3. Read and execute the file
source ~/.bashrc

Mac

Place the alias at the bottom of your .bash_profile file or wherever you prefer.

Starting from macOS Catalina, the default shell for macOS has changed from bash to zsh.
If you are on macOS Catalina and above, choose the path with the **prefix** A, i.e. "A1.". If you are on macOS Mojave and below, choose the path with the prefix B.

***

A1. Edit the .zshrc file in vim
vim ~/.zshrc

B1. Edit the .bash_profile file in vim
vim ~/.bash_profile

***

A/B2a. To make checkstyle alias on all java files in the folder
Add alias to anywhere in the file and save it (w!)
alias checkstyle="java -jar [PATH_TO_FOLDER]/checkstyle-8.2-all.jar -c /[PATH_TO_FOLDER]/cs2030_checks.xml *.java"

ALTERNATIVE A/B2b. To make checkstyle alias on specific java file only
Add alias to anywhere in the file and save it (w!)
alias checkstyle="java -jar [PATH_TO_FOLDER]/checkstyle-8.2-all.jar -c /[PATH_TO_FOLDER]/cs2030_checks.xml"

***

A3. Read and execute the file
source ~/.zshrc

B3. Read and execute the file
source ~/.bash_profile

Proceed to test it out with

  1. If checkstyle alias set up for all java files in the same folder as the checkstyle files
checkstyle
  1. If checkstyle alias set up for specific java file (works for files not in the same folder as the checkstyle files) Go into the folder containing the java file and run
checkstyle [FILE_NAME].java
Clone this wiki locally