Lab 2: command line and checkstyle

Objectives

  • Installing a Java library required for checking your coding style
  • Becoming more familiar with paths and the file system on your computer
  • Practicing logging in and submitting to the submitserver

Review: logging in and downloading an assignment from the submitserver [15 min]

On the testing center laptop, download the starter code for this lab by repeating steps 1 though 5 from last week but clicking on Lab2 this time.

Unzip the lab2_starter.tar file by double-clicking on it like last time. Then, open the file HelloWorld.java in VSCode. Finally, open a Terminal window in VSCode by clicking on the three dots at the top, and navigating to Terminal->new Terminal.

The file system and paths

Computers and servers do not arbitrarily store files somewhere in the cloud; there is an organization scheme for these files, called the file system. Folders and sub-folders are used to organize files in a directory structure (here directory just refers to a folder).

You have direct access to your file system two ways:

  1. Though the Finder application (for Mac) or Windows Explorer (for Windows). This is a graphical representation of the folders and files on your computer
  2. Through your terminal (the small black box where we compiled and ran your code earlier), amongst other places.

Step 1: find your HelloWorld.java on your testing laptop

Use the Windows key (white four rectangles) to open the file explorer app (looks like a filing cabinet at the bottom). Navigate through the folders until you see your HelloWorld.java file there. The path is likely to be Downloads/lab2_starter/HelloWorld.java. Raise your hand if you get stuck.

Step 2: linux commands for navigating the file system

Next, in your VSCode terminal, navigate to that same directory (or the correct one you chose) by using the following commands:

  1. Type ls and hit enter. This lists the contents of the current directory. You should see the Downloads folder there.
  2. Type the change directory command, cd, to navigate to a subfolder of the current folder: cd Downloads
  3. Type ls again, and you should see the folder with the starter code.
  4. Type cd lab2_starter to change into that folder.
  5. Type ls to verify you see HelloWorld.java there. If you don’t, ask a neighbor or TA for help!
  6. Type pwd and hit enter to get the terminal to display your current path in the terminal. It will likely show you something like ../Downloads/lab2_starter/.
  7. Compile and run the file with javac HelloWorld.java followed by java HelloWorld to make sure you see it print out the message.

Step 3: modify and save the file

Now we will make some changes to the file. Underneath the print statement, add the following lines of code:

int x = 3;
int yNum = 4;
int sum = x + yNum;
System.out.println(sum+x);

Save the file; if it is unsaved, you will see a white circle next to the filename. The testing center laptops do not autosave these files; if you make changes, you will need to save and recompile the file to see the changes.

Next, compile and run the code (see step 7 above) and make sure it prints out 10. If not, ask a TA for help!

Finally, add the print statement below to the bottom of your file:

System.out.println(sum);

Save, compile, and run the code and make sure it prints out all three things.

Step 4: running checkstyle

Next we’ll see how good the coding style was for this small file by running a tool called checkstyle. This tool looks over each line of your code, compares it against a template, and produces a list of errors. While the lab quizzes will never check this, most of your homeworks will.

Type ls in the terminal and observe there are two files there:

  • checkstyle-9.2.1-all.jar is a jarfile, which stands for java archive. It’s like a tarfile, but for pre-compiled Java bytecode. This is the checkstyle library.
  • CS1111_checks.xml is a markup file, similar to html. You can open the file and take a look at its contents if you’re curious: it specifies the template for deciding whether or not Java code is written with good style.

With these two files we can run checkstyle on your HelloWorld.java with the following command: java -jar checkstyle-9.2.1-all.jar -c ./CS1111_checks.xml HelloWorld.java You should see error messages similar to these:

Starting audit...
[WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:5:21: Local variable name 'x' must match pattern '^[a-z][a-zA-Z0-9_][a-zA-Z0-9_]([a-zA-Z0-9_]*)$'. [LocalVariableName]
[WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:6:9: 'method def' child has incorrect indentation level 8, expected level should be 16. [Indentation]
[WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:7:9: 'method def' child has incorrect indentation level 8, expected level should be 16. [Indentation]
[WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:8:9: 'method def' child has incorrect indentation level 8, expected level should be 16. [Indentation]
[WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:8:31: WhitespaceAround: '+' is not followed by whitespace. Empty blocks may only be represented as {} when not part of a multi-block statement (4.1.3) [WhitespaceAround]
[WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:8:31: WhitespaceAround: '+' is not preceded with whitespace. [WhitespaceAround]
[WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:9:9: 'method def' child has incorrect indentation level 8, expected level should be 16. [Indentation]
Audit done.

Let’s go through these one-by-one so we have good coding style! Look at the first error message: [WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:5:21: Local variable name 'x' must match pattern '^[a-z][a-zA-Z0-9_][a-zA-Z0-9_]([a-zA-Z0-9_]*)$'. [LocalVariableName] It is reporting where the error is: HelloWorld.java:5:21:. There, 5 refers to the line number, and the error message is saying that variable x must match some pattern. It gives a regular expression, which you will learn about more in CS2113, but it’s basically telling you that x is too short for a variable name and needs to be at least three characters long. Change x everywhere in the file to a better variable name of your choosing. Then, recompile the file using javac, and then re-run the checkstyle command. You should see this error is now gone (there will still be other errors). If the error about x is still there, ask a TA for help!

Next you’ll likely see error messages such as: [WARN] C:\Users\Dr_Kinga\Documents\CS1111_F2025\new_web_site\CS1111_F25\labs\HelloWorld.java:6:9: 'method def' child has incorrect indentation level 8, expected level should be 16. [Indentation] This is telling us that on line 6 the indentation of that line doesn’t match the previous print statement. This might be the case, but for many of you, you might also be getting this error message if everything looks alright. So what’s going on and how do we fix it? It turns out you are probably mixing tabs and spaces in the identation you use on those lines. VSCode displays a tab as four spaces with, which is why the human eye can’t see the difference.

Fortunately, there is an easy way to fix this everywhere you do it in your file! In VSCode, go to Edit->Replace and a small box will pop up in the right top of your screen. In the Find box, type in four spaces. You should see VSCode automatically highlight all instances of these four spaces in your code. Notice these are the same line numbers that were giving you errors! Next, in the Replace box, type \t (a backslash followed by a t). This is a regular expression/escape sequence that computers understand for the tab character. Then, click the .* icon to turn on regular expressions. Finally, click the second icon to the right of the Replace box to Replace All instances of four spaces with a tab. The code will look the same, but all the highlights for what would have been four spaces are now gone! Save the file and re-run checkstyle and you should have two errors left. Hint: you can use the up arrow on your keyboard to re-run recent terminal commands (keep pressing up until you see the command you want).

These last two errors are both complaining about spaces around the + operator. Add a space in around each side, save the file, and re-run checkstyle. If all goes well, you should see the following: Starting audit... Audit done.

Step 5: submitting you file for participation credit

Like last time, run the command at the top of HelloWorld.java file to create the lab2.tar file. Then, upload this file under the Lab2 link on the submitserver. Hit refresh and verify that you scored a 100/100. If not, ask a TA for help!

Installing checkstyle on your laptops

We just completed an exercise to get more practice downloading and running files from the submitserver on the testing laptops: you’ll need this knowledge for your live coding quizzes!

However, the actual checkstyle tool will only be used on the homeworks. So, repeat steps 1-5 on your laptop to make sure you are able to successfully run and use checkstyle there. There is nothing to turn in for this component, but you should do it to 1) add hours towards that 10,000 hours of practice – knowing how to navigate the file system and run the java commands is crucial in order to be a good programmer and 2) make sure you will have no problems running checkstyle at home.

If you finished this lab early, continue to work on the Homework1 problems, as these are due soon.