Skip to Content
Course content

6: Setting Up IntelliJ IDEA for Java Development

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've probably already installed IntelliJ IDEA, written a few lines of code, and then hit a wall. It's a rite of passage. You look at your screen and see something like this:

public class Greeter {
    public static void main(String[] args) {
        System.out.println("Ready to go!");
    }
}

The code is syntactically perfect. There isn't a single missing semicolon. But in IntelliJ, String and System are highlighted in bright red. When you try to run it, the "Run" button is either greyed out or you get a vague error saying "Project SDK is not defined."

Why your code is red even though it's right

When this happens, it's rarely a problem with your Java knowledge; it's a communication breakdown between the IDE and your computer. IntelliJ is a powerful editor, but it's just an editor. It doesn't actually "come with" Java. It needs to know where the Java Development Kit (JDK) is installed on your hard drive so it can use the standard libraries to understand what a String actually is.

If the Project SDK isn't set, IntelliJ is essentially guessing. It sees System.out.println and thinks, "I've never heard of a class called System," because it hasn't been told which version of the Java library to look at.

Connecting the Engine to the Editor

To fix this, we need to point IntelliJ to your JDK. I've found that the fastest way to handle this is through the Project Structure menu. Go to File > Project Structure (or press Ctrl+Alt+Shift+S if you want to feel like a power user).

  • On the left sidebar, click Project.
  • Look for the SDK dropdown. If it says "No SDK," that's your culprit.
  • You can either select a JDK you've already installed from the list or, even easier, click Add SDK > Download JDK.
  • I usually recommend the Amazon Corretto or Oracle OpenJDK distributions—they're stable and industry standards.

Once you hit "OK," you'll notice the red text vanishes almost instantly. The IDE has finally "indexed" the library, and now it knows exactly how String and System behave.

Taming the Auto-Imports

One of the things that drives me crazy when I start a new project is having to manually type import java.util.ArrayList; every single time I use a list. You don't have to do that. IntelliJ can handle the imports for you while you type.

Go to Settings > Editor > General > Auto Import. Check the boxes for "Add unambiguous imports on the fly" and "Optimize imports on the fly." Now, when you type ArrayList and hit Enter, IntelliJ will silently add the import statement at the top of your file. It keeps your code clean and saves you from a lot of tedious typing.

Using the Gutter to Run Code

You'll see a lot of tutorials telling you to find the "Run" menu at the top of the screen. Honestly? Forget that. Look at the "gutter"—the thin vertical strip to the left of your line numbers. Whenever you have a public static void main method, a small green play arrow appears right next to the line number.

Click that arrow and select Run 'ClassName.main()'. This is the fastest way to launch your program. The first time you do this, IntelliJ creates a "Run Configuration" for that specific class, which is why you'll see it appear in the top-right dropdown menu for subsequent runs.




📋 Practical Task

Building a Currency Converter Project

Now it's time to verify your environment is fully configured. Instead of a simple print statement, you are going to build a small project that utilizes external classes to ensure your SDK and auto-imports are working correctly.

Your Goal: Create a new project in IntelliJ and implement a CurrencyConverter class.

  • Project Setup: Create a new Java project named CurrencyConverter. Ensure you have selected the JDK you configured in the lesson.
  • Implementation: Inside your main class, use a java.util.Scanner to take a double value (the amount) from the user via the console.
  • The Logic: Multiply that value by a fixed exchange rate (e.g., 1.1 for USD to EUR) and print the result.
  • Verification:
    • Ensure that Scanner is automatically imported (or manually imported if you prefer) without any red text.
    • Run the program using the green play arrow in the gutter.
    • Verify that the output displays correctly in the IntelliJ Run tool window at the bottom.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.