Skip to content

Setting Up CheckStyle

frisciliasultan edited this page Oct 6, 2020 · 11 revisions

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.35-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.

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

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 ~/.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