# **CS200 Java Stencil Setup Guide**
This guide contains instructions for downloading and running Java stencil code into IntelliJ. You can use this process to load example Java code posted for a lecture, or to get started with homeworks/projects/labs in Java.
# **Before you start**
This guide assumes that you have set up IntelliJ and Git on your system, which was part of HW0. Specifically, if you have not done so already, you should install and configure IntelliJ per the [**IntelliJ guide**](https://docs.cs200.io/s/intellij-guide). If you encounter errors, remember to check the Troubleshooting section.
1. **Install and configure IntelliJ per the** [**IntelliJ guide**](https://docs.cs200.io/s/intellij-guide) up to the “Running assignments” section. If you encounter IntelliJ errors, remember to check the [FAQ section](https://docs.cs200.io/intellij-guide?both#Common-BugsFAQ).
2. **Use the** [**Github guide**](https://docs.cs200.io/s/github-guide) **to create a Github account, install git on your system, and generate an Access Token** (not needed if you're in class right now and you just want to download lecture code)
3. **Create a CS200 folder on your computer** to hold all of your assignments and example code. We recommend putting it on your Desktop (Mac) or or C: Drive (Windows) for ease of access, but you can use any path that you can find easily. **If you already made a CS200 folder, take a moment to find it now.**
# **How we provide code**
There are two ways we might give you code to run on your system:
- For **Homework/Project/Lab stencils**, we will give you a **Github Classroom link** (eg. `https://classroom.github.com/<something>`). To use these, see [**Accepting Github Classroom Assignments**](https://docs.cs200.io/s/github-guide#Accepting-an-Assignment-on-GitHub-Classroom)
- For **HW0, lecture code, or other examples**, we will directly provide a direct **Github Repository URL** (`eg. https://github.com/<something>`). To use these, see **[Cloning from a Repository URL](https://docs.cs200.io/s/github-guide#Cloning-a-repository-by-URL)**.
### **Accepting an Assignment on GitHub Classroom**
<!-- DO NOT REMOVE THIS SECTION: yes, it's redundant, but other guides may link here -->
See our Github Guide, [here](https://docs.cs200.io/s/github-guide#Accepting-an-Assignment-on-GitHub-Classroom).
### Cloning a repository by URL
<!-- DO NOT REMOVE THIS SECTION: yes, it's redundant, but other guides may link here -->
See our Github Guide, [here](https://docs.cs200.io/s/github-guide#Cloning-a-repository-by-URL).
# **Opening a Stencil in IntelliJ**
4. To open your project, open IntelliJ and go to **File > Open** and browse to the folder you cloned in the previous step. Select the folder and click **Open.**
5. If IntelliJ displays a warning about security settings, click **“Trust Project”** to allow IntelliJ to open the code. From there, IntelliJ should open its main window, which looks like this:
![](https://lh4.googleusercontent.com/mgVi1t-yP_esAXet5iEQHl6dQXTTEdkkwore794Xhn0JYHQk-SUqtdKucxTn23SFlP29jo8ra4nv_jT932ZDotbE5ep0R1BsMJ-fyw7Xs78lHSsCTm28epL_eGCe-7CSTtVCkjXF1W1ExHwXoG54FdU)
6. If you’re new to IntelliJ, this might look like a lot. That’s okay! We’ll learn more about the different parts in the coming weeks. For now, look at the sidebar on the left, which shows all the files in the project. **Expand the** `src` **folder (and** `sol`**, if it’s there)**--here you will see the Java files you’ll be using.
:::warning
:eyes: **If your files disappear after a few seconds, or you don't see any files**: The first time you open a new project, you might notice the files in your sidebar disappear and reappar, or IntelliJ running a bit slowly. This is a normal part of IntelliJ's first time setup process: if your files list disappears, just expand the list again from the little arrow on the left.
![](https://docs.cs200.io/uploads/f5507c55-5e61-47fb-a25f-d293d9fa4843.png)
**If you don't see any files, look for a prompt or error message** in the bottom-right of the IntelliJ window asking you to run Gradle build scripts. If you see this, click yes or "Run Script" and then your files should show up.
:::
7. When downloading any new project, the first step is to make sure the project is set up correctly. To do this, open any file with `Test` in the name (eg. `AnimalsTest`, in the image above) , which contains code that’s easy to run–we’ll use this to test our IDE settings.
8. At the top of the file, you may see a yellow bar at the top saying “Project JDK not defined.” If so, click **“Setup SDK”** and select the highest version of Java available on your system (17 or higher), like this:
![](https://lh3.googleusercontent.com/1lD0zuJ1cDjMKFpPAwgTS_fVFOqKQA553ouTJXqIBsOvzuoh-1xCFjcVLzlX_ESlNVsQUReS1aUPuG5vo-fiZH6vKlVK_2zB70H--X_euzd9F7gZIWqZdVoXwFFRWQUeQgAzojsfPIeqyCf9-MK8V5Y)
9. Once you’ve set your SDK, IntelliJ should start automatically building and checking the project–this may take a minute or two, depending on the size of the project–you should see a progress bar at the bottom-right of the IntelliJ window that will tell if it’s still working.
10. Once IntelliJ is done setting up, you should see any red underlines in the code disappear. This means the code is ready to test! To run the tests, **look at the top of the test file for a green icon** (circle or arrow) next to a line labeled `public class SomeThingHereTest { `like this (we’ll talk about what it all means soon!):
![](https://lh3.googleusercontent.com/9yEGWn2sEceipBNcy51VjYcbxpE0vZ4H9583smKZ8Lj8Tq-yq_71uLlop-AjbLVjQmqmeqWe3fBLkIwCzdX0gg12kHdgbLGH4zAgLGA7Ojnc33951XzWjByulo-6rFOEsliPJejps_2owLxaYYBjvvM)
:::info
**_Note:_** **If you don’t have a green icon, or the code has lots of red underlines:** make sure you have set your SDK (step 9).
**If you still have issues, this might be an issue with the stencil project**–check the [FAQ section of the IntelliJ guide](https://docs.cs200.io/intellij-guide?both#Common-BugsFAQ) or the IntelliJ setup megathread on Ed to see if any of the issues match your situation. If you still have questions, feel free to post in this thread on Ed or ask us in hours!
:::
11. Click the green icon and select **Run** `SomethingHereTest` (where `SomethingHereTest` is the name of the file)
12. If all goes well, IntelliJ will build the project code and run any tests–this may take a minute. Once it’s done, a window should pop up showing the status of the tests. **You don’t need to know what the output means yet–we’ll unpack it all over time in class.** For now, just look for a window to pop up like one of these:
**Example with passing tests**
![](https://lh3.googleusercontent.com/TWXq2eZ0L5_4b05Oyj4VfNVNK0PM0QM-waJx5iVEQGJiE-pe_CWxcxGonRsrBwz_CGyHGx7gfskc8t4MurksMcq14YAtUDY2TmcIQ-NiKhlUjIOcPax_yFH852v_8ufSV71PQPCKD85UucHiyBG_2Yk)
**Example with failing tests (okay for now!)**
![](https://lh5.googleusercontent.com/5uuWIE48Ks8hcJxU61wUCticCGxia0R7Aj5sP9CDVK2Px0nrrQUSTIPIUeTfqVep--fZ_tJh2sWIqeS_q115vUQb-63ZVwGxHnysWBrvE4bChaFtRz05nnKuNaiPFmxbA2J5kUqmtdJGZtoO57L-kFQ)
13. **You may see some tests listed as failing–this is okay!** This will occur in stencil code, usually because there are some components that haven’t been implemented yet! For now, we just need to run the tests, which proves our IDE is working, yay!
14. If you were able to run the tests (whether they pass or fail), you’re done! You have now successfully tested running code in your IDE! \
**If IntelliJ produced an error instead of running the tests, there may be an issue with your IDE, or the stencil project itself.** Check the [FAQ section of the IntelliJ guide](https://docs.cs200.io/intellij-guide?both#Common-BugsFAQ) or the IntelliJ setup megathread on Ed to see if any of the issues match your situation. If you still have questions, feel free to post in this thread on Ed or ask us in hours!
# Troubleshooting
## Known issues and solutions
### LOTS of red underlines :sob: / Unsupported class version...
If nearly everything is underlined in red, and you haven't changed anything since you downloaded the code, this is a sign that your Java version might not be set correctly. To fix this:
- Be sure to [Check your SDK version](#Configure-your-SDK-version)
- [Check your Gradle JVM](#Set-your-Gradle-JVM)
- If none of the above help, also try [Invalidating your caches](#Invalidating-your-caches)
### Cannot find module / Cannot find symbol...
This suggests that Java doesn't know how to find a certain class:
1. To start, hover your cursor over the class name that can't be found and look at the box that pops up.
2. If you see a line **Import class..."**, click it to have IntelliJ add an `import` statement to the current file, which should should resolve the error
3. If you don't see an "Import class..." message, or if the error doesn't go away, [check your SDK version](#Configure-your-SDK-version)
## Configure your SDK version
If your stencil settings are configured incorrectly, you may need to configure your stencil project's SDK version, which tells is the correct Java version to use. To do this:
1. With your stencil project open in IntelliJ, go to **File** > **Project Structure**
2. In the window that pops up, select "**Project**" from the sidebar on the left (as in the figure)
3. Set the **SDK** field to **any SDK version 17 or higher** (eg. OpenJDK 20, as shown in the figure). If you don't have an suitable option on your system, select **"Download JDK..."** to get one.
4. Set the **Language level** to **any version 17 or higher**
5. Click **OK**.
![](https://docs.cs200.io/uploads/8a3760bf-a495-44d9-af69-1c0f93f956ea.png)
After saving your setings, you should see any red underlines go away if they were caused by an incorrect Java version. If they don't go away immediately, try [Invalidating your caches](#Invalidating-your-caches).
## Set your Gradle JVM
In some situations, you may need to configure the Java version used by Gradle, which is a tool that we use to configure our stencils. We can do this as follows (as shown on the figure below):
1. Open your IntelliJ settings by selecting the menu **IntelliJ Idea > Settings...** (MacOS) or **File > Settings...** (Windows, Linux)
2. In the sidebar of the window that pops up, expend the sections **Build, Execution, and Deployment** > **Build Tools** > **Gradle** (like the figure below)
3. In the **Gradle JVM** field at the bottom, select **any JDK 17 or higher** (eg. OpenJDK 20, as in the figure)
4. Click **OK**.
![](https://docs.cs200.io/uploads/b5c9ed6b-a57f-486b-84ab-d79e41e8a68a.png)
After changing your settings, try to run your tests again--you should now be using the correct JDK version.
## Invalidating your caches
If you've recently changed some IntelliJ settings but you're still receiving errors, you can "invalidate your caches", which clears out old IntelliJ state that might still have the wrong settings. To do this:
1. Go to **File** > **Invalidate Caches**
2. Select the first two options in the list (like the figure below)
3. Click **Invalidate and Restart**
![](https://docs.cs200.io/uploads/7a0f71eb-3a87-49fb-8025-230d2603d185.png "Invalidate Cache Image" =400x)
## Rebuilding your IntelliJ project settings (advanced)
As a last resort, you can delete all your project settings and create them again from the defaults. To do this:
1. In the project files view, hold down the **Command** (MacOS) or **Ctrl** (Windows) key and select the `.gradle` `.idea` and `build` directories. (If any of these are missing, that's fine.)
2. Delete these directories: from the IntelliJ menu, select **Edit** > **Delete**.
3. **Close and reopen** your current IntelliJ stencil folder.
After IntelliJ reopens, your files will appear to be missing! This is fine, they're actually still there--we just have one more step to do:
4. Once IntelliJ reopens, you should see a box pop up in the lower-right corner that says "Gradle build scripts found", like the figure below. Click **Load Gradle Project** to reload your settings!
5. Once IntelliJ finishes reloading, a folder for your stencil should reappear in the sidebar. Expand the project folder to see your files:
![](https://docs.cs200.io/uploads/f5507c55-5e61-47fb-a25f-d293d9fa4843.png)
Your project settings should now be rebuilt! If you needed to do this earlier, you may need to [Reset your SDK version](#Configure-your-SDK-version) or [Check your Gradle JVM](#Set-your-Gradle-JVM).
***
_Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CSCI0200 document by filling out the_ [_anonymous feedback form_](https://forms.gle/8HY7tDdgM98fk2e19)_._