Generic Top

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

A good IDE is important for developer productivity. IntelliJ is currently one of the leading IDEs and supports many programming languages.

In this tutorial, we’ll start with some of the basic configurations in IntelliJ, focusing on the Java programming language. We’ll also list the most common shortcuts in IntelliJ for boosting developer productivity.

2. Installing IntelliJ

First, we need to download and install IntelliJ for our platform. For the features that we are going to go over, either the Ultimate or Community edition will do great.

3. Basic Project Configuration in IntelliJ

3.1. Configuring JDK

IntelliJ is written in Java and comes with a packaged JRE for running the IDE.

However, we’ll need to configure IntelliJ with a JDK to do any Java development. It can be configured either globally or per project.

First, let’s see how to configure a global JDK using the Switch IDE Boot JDK option:

The easiest way to find the Switch IDE Boot JDK option is from the “Find Action” wizard.

We can get there from the Help menu or by typing Ctrl+Shift+A or Cmd+Shift+A. Usually, it will list every installed JDK and allow us to choose the desired one.

Next, we’ll create a new Java project.

3.2. Creating a Java Project

In order to create a new Java project, let’s bring up the New project wizard from File->New->Project:

 

Next, we’ll select Java in order to create a simple Java project.

Additionally, this window allows us to configure a project-specific JDK if we want to.

On the next screen, IntelliJ provides template projects like Hello World as a starting point, but let’s just select Finish and get started.

Now that we have a basic project structure, we can add a Java class by selecting the src folder and then either right-clicking or typing Alt+Insert. We’ll select Java Class from this menu and get a dialog where we can give it a name:

3.3. Configuring Libraries

A Java project usually depends on a lot of external or third-party libraries. And while Maven and Gradle are the typical go-tos for managing this, let’s take a look at how to do this natively in IntelliJ.

Let’s say we want to use the StringUtils API from the commons-lang3 library.

Like the JDK settings, we can also configure libraries at global and project level. Global libraries are shared by all projects. Both global and project specific libraries can be added by accessing the Project Structure dialog (File->Project Structure).

In order to add the library, we must download it first. Normally, the common source for any external library is the Maven Repository. Hence, IntelliJ allows us to download it directly from any pre-configured Maven repository. And of course, if no repository is configured, it will search the Maven Central.

IntelliJ will now download the commons-lang3.jar into a specified folder. Along with that, it also adds it to the project classpath.

Of course, remember that adding a library this way is IntelliJ-specific and not as portable as more robust options. It’s convenient, though, for simple projects.

In the next section, we’ll use this library and execute a simple Java program.

4. Running or Debugging an Application

4.1. Run/Debug Configurations

Before we run our Java program, let’s add some code to the class we added earlier. We’ll simply use the added library and call StringUtils.reverse() to reverse any text given as a program argument:

System.out.println(StringUtils.reverse(args[0]));

Now, there are 2 approaches for running this main method in IntelliJ. Firstly, we can simply run Ctrl + Shift +F10 or Control + Shift + R/D from the main class. IntelliJ will then create a temporary Run configuration.

However, since we have to pass a String to our StringReversal application as a program argument (the args[0] part), a temporary run configuration won’t work.

So, we can create a permanent Run/Debug Configuration.

We’ll do that using the “Edit Configurations” window from the Run Navigation bar (Run->Edit Configurations):

Here, we specify the name of our class to run in Main Class. It needs to have a main method for this to work.

We’ll also pass a String – baeldung, in this case – as a Program Argument to our application.

And, while we won’t demo this here, we can also configure JVM options and environment variables, too, for our application.

Contrary to temporary run configurations, IntelliJ saves this configuration and allows us to execute it any time with a click of a button.

4.2. Debugging a Java Application

IntelliJ has great support for debugging many languages. Let’s debug our String Reversal utility as an example.

As with most IDEs, we can add a breakpoint on any line of our class from the editor by clicking on the side panel:

Now, we can debug the class by clicking on the debug icon from the Run/Debug configuration.

In this case, the program is suspended at line 9 as shown above, allowing us to inspect the thread stack, inspect variables or even evaluate expressions (Alt+F8 or Option/Alt + F8).

At this point, we can either Step Into (F7) the StringUtils.reverse() method, Step Over (F8) the line or Resume Program (F9), meaning run until either the next breakpoint or until the end of the application.

Usually, most IDEs allow the users to mark a line in a Java class as a breakpoint like we just used. In addition, IntelliJ allows us to configure more than just Line breakpoints. We can also do:

  • Temporary Breakpoint – A line breakpoint which is executed only once
  • Exception Breakpoint – A breakpoint on any exception class in Java. The debugger will pause when that exception is about to be thrown
  • Method Breakpoint – One that executes when entering or exiting a method
  • Field Breakpoint – And one that executes when a field is modified

A breakpoint can have conditional logic, too.

We can view and configure all the breakpoints in a project in the Breakpoints dialog Run->View Breakpoints (Ctrl+Shift+F8 or Cmd+Shift+F8).

4.3. Building Artifacts

Now that we’ve tested, debugged and fixed all the issues, we are ready to ship our application. Therefore, we need to create deployable binaries for our application.

We can create deployable .jar binaries in IntelliJ automatically. 

First, in the Project Structure (Ctrl+Alt+Shift+S or Cmd+;), we need to declare a new artifact.

We select “Artifacts”  and then click the plus button.

Next, we select a JAR artifact and also add dependencies in the JAR:

Next, we’ll go back to our Run/Debug Configuration dialog.

There, we need to add a Build Artifact task in the Before Launch window. As a result, a new executable jar is created for our application every time we execute our Run/Debug configuration.

Again, building artifacts is not IDE-agnostic. This mechanism is specific to IntelliJ. A build management tool could be a better approach, similar to what we discussed for dependency management.

5. Common Shortcuts in IntelliJ

The shortcuts are really useful in boosting developers productivity. The following is a quick cheat sheet for the most common ones.

5.1. Navigation

  • Search Class – Ctrl + N / Cmd + O
  • Search All files – Double Shift
  • Recent Files – Ctrl + E / Cmd + E
  • Switch between files – Ctrl + Tab / Cmd + Tab
  • Type Hierarchy – Ctrl + H / Control + H
  • Call Hierarchy – Ctrl + Alt + H /  Control + Alt+ H
  • File structure popup – Ctrl + F12 / Cmd + F12 (lists all methods and fields)
  • Go to declaration – Ctrl + B / Cmd + b
  • Go to implementations – Ctrl + Alt + B / Cmd + Alt+ B
  • Show Project Structure – Ctrl + Alt + Shift + S / Cmd + ;

5.2. Editor

  • Code Completion – Ctrl + Space / Control + Space
  • Method parameter info – Ctrl + P / Cmd + P
  • Method/Class documentation info – Ctrl + Q / Control + J
  • Reformat Code – Ctrl + Alt + L / Cmd + Alt + L
  • Optimize imports – Ctrl + Alt + O / Control + Alt + O
  • Duplicate line – Ctrl + D / Cmd + D
  • Delete line – Ctrl + Y / Cmd + Delete
  • Code selection – Ctrl + W / Alt + Up
  • Show quick actions – Alt + Enter / Alt + Return
  • System.out.println  sout + Ctrl + Enter / sout + Control + Space
  • public static void main  psvm + Ctrl + Enter / psvm + Control + Space
  • System.out.println for a method’s return value methodName().sout [Enter]
  • Assign method’s return value to local variablemethodName().var [Enter]

5.3. Refactoring

  • Rename class/method – Shift + F6
  • Extract Method – Ctrl + Alt + M / Cmd + Alt + M
  • Extract variable – Ctrl + Alt + V / Cmd + Alt + V
  • Extract field – Ctrl + Alt + F / Cmd + Alt + F
  • Extract constant – Ctrl + Alt + C / Cmd + Alt + C
  • Extract parameter – Ctrl + Alt + P / Cmd + Alt + P

6. Conclusion

In this article, we looked at some basic configurations in IntelliJ.

As an example, we created a Java project, added libraries, debugged it, and created an artifact, all in IntelliJ.

Lastly, we looked at shortcuts for some common actions.

Generic bottom

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Loredana CrusoveanuDenis Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Denis
Guest
Denis

Hi Folks,

I love IntelliJ for the shortcuts it provides when we want to wrap the return value for sysout. It’s done like: methodName().sout[enter] or when assigning return value to local variable: methodName().var[enter].

I believe it might be worth to mention.

Another thing is live templates, but it probably requires separate article.

Cheers

Loredana Crusoveanu
Editor

Hi Denis,

Thanks 🙂 I’ve added your suggestions to the Shortcuts section.