(Credit for this document goes to Tim Kutcher, TA from 226Spring19)
Hey all,
Installing
- With an edu email, you can create a “student account” to get a license to use the “Ultimate Edition”
- Go here: https://www.jetbrains.com/student/
- Click “Apply Now” and create a username+password that you will remember
- You can go here to install it: https://www.jetbrains.com/idea/download/
- Go through accepting all the default install settings, no need to set up anything fancy
Project Setup
- First, it is important to understand a big IDE like this thinks of everything as “projects” - you can’t really just use it to open your HW1 folder and develop that code (it will be a little confused), so we’ll want a better setup.
- Make a
cs226/
folder somewhere you want to have everything for the class.
- Inside
cs226/
, you can add a homework/
folder
hw1/
for example will go inside this homework/
folder
- You might want to make a
resources/
folder or something to put our cs226_checks.xml
in
- Add one more folder inside
cs226/
and name it classes/
(more on this later)
- Now, in IntelliJ, you can open the
cs226/
folder. You will always open this folder.
- Near the top left, there should be a tab or something named “Project”, click it
- This should show you the directory structure we already created
- Navigate to any java file
- You might see the “Project SDK is not defined” message
- If so, click “Setup SDK” and hopefully you have the option to choose “1.8” (if not, it’s a little trickier and you might just need to stop by my office hours)
- Go to File
→ Project Structure - Inside Project Settings select Project - Your Project SDK should be set to 8 - In the Project language level select 8 - For Project compiler output click the little folder icon then select the classes/
folder we made - IntelliJ basically wants to know where to write the .class files too when compiling, and know where to look for them when running - So we are just telling it where we want to keep our class files (separate from our source files) - Hit Apply and OK - The last thing we need to do is tell IntelliJ where our java files are - Right-click on homework/
folder you created and then Mark Directory as… → Sources Root - Now, that folder should turn blue, and the hw1
folder should have the little package icon. - Now all the java files should be able to find each other, etc. - The classes
folder should be red - At the bottom, if you select Terminal you should get a shell at the bottom open to the cs226
folder.
Compiling and Running
- To compile, on the top ribbon just click the hammer icon
- If you get any compile errors, they will highlight red in the source file and you will have the compiler messages down at the bottom
- You can double click on any of the messages and it will take you to where the error is referring to
- To run a main method, such as PolyCount, scroll down to the actual main method in PolyCount
- Next to it, there should be a “Run” icon (looks like a play button)
- Click that and it should run, but the problem is we didn’t provide any arguments/flags (like
-ea
to enable asserts)
- Up top, next to the build icon, there is a little dropdown box displaying PolyCount.
- Drop down and select Edit Configurations
- On the left hand side, select the PolyCount “configuration”
- In the VM Options, add
-ea
.
- Click Apply and OK and now, whenever you run PolyCount (by clicking the run icon up top), asserts should be enabled
- Add an
assert false;
statement somewhere just to verify
- For a program like Unique, you could follow the same strategy as above (and add arguments to the Program arguments rather than the VM Options bar), but that may get hard to debug/test
- Instead, open the terminal and navigate to the
classes/
directory
- From there, navigate to one level above the
hw1/
folder (the one inside the classes/
folder)
- Now you can run
java hw1.Unique
as normal from the command line
- IntelliJ also has a nice debugging tool
- To set a breakpoint, click in the gray area to the right of the line number
- By default, running won’t stop at breakpoints
- Instead of clicking the Run icon, click the little Bug icon next to it
- Now you should actually be able to inspect variables and whatnot at those breakpoints
Integrating Checkstyle
- To add checkstyle, you need to first add it’s plugin
- In Preferences/Settings, on the left hand side select Plugins
- Search Checkstyle
- Depending on your installed version of IntelliJ, you might need to click Browse Repositories… before you search
- Click the install button
- Once it installs, you will be prompted to restart IntelliJ
- After IntelliJ restarts, we can add our configuration file to the tool
- Go back to Preferences/Settings
- On the left hand side (probably under Other Settings), you should now have a Checkstyle option
- Click that and you’ll see the configurations it comes with
- Click the “+” icon to add another one
- For description, enter something like “cs226 Checks”
- For the file, select the
cs226_checks.xml
file within our resources/
folder
- Click the Store relative to project location check box
- Click the next/finish button and you should get a message that it was verified
- Now you should see it in the list of configurations
- Select the checkbox next to it to make it “active”
- Click Apply, and OK
- Our configuration file should be set up now
- To run checkstyle on a file, go to that file
- At the bottom, click the CheckStyle tool on the ribbon
- To run, click the run icon
- You can double click on any errors and it will take you to the error
- In the actual editor it will underline any checkstyle error and hovering will tell you what is wrong
JUnit
First, download the junit-4.12.jar and the hamcrest-core-1.3.jar files to somewhere within your project (a resources folder or something)
Go to File → Project Structure → Modules → Dependencies
At the bottom of the window, click the plus icon and then select JARs or directories...
This is analogous to the -classpath
/-cp
argument from the command line
Navigate to and click the two jar files mentioned above
Now click Apply and OK and you should be able to compile with everything
To run JUnit tests, you can click the little play icon next to any of the @Test annotated methods to run that test
Clicking the little play/fast-forward icon next to the class definition will run all @Test annotated methods in the class
When you run, you’ll get a display of which tests passed and which failed
Git
- Git tools automatically come with IntelliJ
- If you create a repository for the
cs226
level, it will automatically recognize that there is a git repo for the project
- From terminal at the
cs226
level run git init
, and git remote add origin <url-to-repo.git>
git pull origin master
will get any file from the repo
- You’ll want to add some things to a
.gitignore
file because you don’t need to track them
- First, you can ignore the
classes/
directory
- Second, you can probably ignore the entire
.idea/
directory (it’s just IntelliJ config stuff)
- If you want to keep some of that, copy this in to your .gitignore file
- You can probably also ignore any jar file (
*.jar
).
- Right-clicking on a directory, you can select Git and then Commit Directory… (or do the same thing for a specific file)
- Enter a commit message
- You’ll probably want to un-check the Perform code analysis option
- You can click Commit at the bottom
- Or you can click the dropdown and the Commit and Push…* to push it
- You can also just use git from the terminal since that might be a little quicker
- Files will be colored based on git
- No color: no changes/has been committed
- Blue: changes not yet staged
- Green: new file
- Grey: ignored
Editing
These are just some wildcard cool-tricks to use while editing - If anything is highlighted or red, option/alt+click will give you options to fix - Command+click on any variable will take you to that variable declaration - Command+click on any type/class will take you to that class - Command+click on any method call will take you to that method - If you want to change a variable or method name, right click on it and select Refactor… and Rename - For Javadoc, if you type /**
above the method and hit enter/return, it will give you skeleton Javadoc - At the bottom you can click TODO
and it will give you a list of everywhere there is a TODO item. For homeworks you want to make sure you do all of these. - If you use markdown for your README, as you write in a .md
file it will automatically show you the render next to it.