IntelliJ

Overview

InjelliJ has good support for maven. It is very easy to run all the maven lifecycle phases from within the IDE - which makes it very convenient during the development process.

Eclipse has the same convenient features - with the installation of a plugin. Look at the Eclipse Tips document for more details. But working with maven is not as easy in Eclipse.

Installation

See Installing different products from Jetbrains

Setting spring profiles in IntelliJ when running applications

https://stackoverflow.com/questions/39738901/how-do-i-activate-a-spring-boot-profile-when-running-from-intellij

With IntelliJ Community edition

  1. Goto Run > Edit Configuration
  2. Choose the configuration you want to edit, in the left under Application.
  3. On the right side > Under Environment Variable, update spring.profiles.active=<your profile name> example spring.profiles.active=dev (Note: the variable should be without -D flag)
  4. Save the changes and Run the Spring boot app with that configuration.

Another solution

Other people mentioned this worked for them. Maybe, they were using newer versions of the IDE.

  1. Add -Dspring.profiles.active=test to VM Options and then re-run that configuration.
  2. Choosing Run | Edit Configurations…
  3. Go to the Configuration tab
  4. Expand the Environment section to reveal VM options

Always show menu bar

File -> Settings -> Appearance % Behavior -> Appearance -> UI Options -> Show main menu in a separate toolbar

Keyboard shortcuts

Command Description
Ctrl [ or Ctrl ] To go to the start or end of the method
Navigate -> Back Get back to the previous location
Navigate -> File Structure Outline view (equivalent to Ctrl O in Eclipse)
Alt + Up arrow (Ctrl Shift Up arrow in Mac) Jump to the method above
Alt + Down arrow (Ctrl Shift Down arrow in Mac) Jump to the method below

Gotchas with renaming in IntelliJ

The refactoring capabilities are nice but we have to be very careful with it. I refactored the name of a method. This method was used in multiple projects. It renamed this method and a related method as well - leading to unintended behavior.

Renaming variable names and method names in a file is ok.

But for renaming method names, you are probably better off doing it manually.

My complaints about the way Git works in IntelliJ

If you stage some changes using Git in IntelliJ, when you look at the status from terminal, why doesn’t terminal reflect the updates done from IntelliJ Git? Vice versa is also an issue. If you stage some changes from terminal, whey don’t they show up as staged in IntelliJ?

I staged some files using terminal but IntelliJ is not showing that file as staged

IntelliJ IDEA may not immediately reflect changes made to Git’s staging area directly through the terminal due to its default use of “Changelists” and its own internal synchronization mechanisms.

To address this and ensure IntelliJ reflects the staged files, do the following

Enable Git Staging in IntelliJ IDEA:

  1. Open IntelliJ IDEA Settings/Preferences (Ctrl+Alt+S or Cmd+,).
  2. Navigate to Version Control | Git.
  3. Select the Enable staging area checkbox.
  4. This will switch IntelliJ’s Git integration to use the native Git staging area concept, aligning it with command-line behavior.

Synchronize IntelliJ IDEA with the File System:

  1. Right-click on the project root in the Project tool window.
  2. Select Synchronize (or use the shortcut Ctrl+Alt+Y / Cmd+Alt+Y).
  3. This forces IntelliJ to rescan the project files and update its internal view of the version control status.

Check the Commit Tool Window:

  1. Open the Commit tool window (Alt+0 or Cmd+0).
  2. With Git Staging enabled, you should now see the staged files listed under the “Staged” section, reflecting the git add operations performed in the terminal.

Issues and debugging tips

Exception: intellij failed to resolve org.junit.platform:junit-platform-launcher:1.3.2

Option 1:

Intellij IDEA > Preferences… > Appearance & Behavior > System Settings > HTTP Proxy

Changed from No Proxy to Auto-detect proxy settings (Or, if you know the proxy settings for your company, enter them there)

Option 2:

It turns out that, junit5-platform-launcher dependency needs to be added in order for Junit5 tests to run in IntelliJ.

As a workaround, add this dependency explicitly in pom.xml, and it will solve the issue.

<dependency>
     <groupId>org.junit.platform</groupId>
     <artifactId>junit-platform-launcher</artifactId>
     <scope>test</scope>
</dependency>

You can remove the dependency from the pom.xml after IntelliJ downloads the jar successfully. This change doesn’t have to be committed to source control.

Caused by: java.lang.ClassNotFoundException: kotlin.Result

When we try to run a Java program in debug mode, we may see this error.

Resolution: https://stackoverflow.com/questions/75249847/intellij-idea-debug-mode-exception-in-thread-main-java-lang-classnotfoundexce

Settings (Preferences on macOS) | Build, Execution, Deployment | Debugger | Kotlin | Disable coroutine agent

or

Settings (Preferences on macOS) | Build, Execution, Deployment | Debugger | Kotlin | Attach corouting agent (unselect this option)

How to open multiple projects in one window in IntelliJ?

https://www.jetbrains.com/help/phpstorm/open-close-and-move-projects.html#merge-project-windows

Merge project windows (macOS)

On macOS, you can merge all opened project windows into one, turning them into tabs.

Make sure that there are several PhpStorm projects opened in separate windows.

In the main menu, go to Window | Merge All Project Windows.

Drag a project’s tab to work with the project in a separate window again.

With default macOS settings, tabs will be lost after you restart the IDE, and projects will open in separate windows. If you would like the IDE to reopen projects in tabs, configure your macOS to always use tabs when opening documents.

(This is Apple specific configuration. Not specific to IntelliJ.)

https://support.apple.com/guide/mac-help/use-tabs-in-windows-mchla4695cce/mac

Specify when to open documents in tabs

On your Mac, choose Apple menu > System Settings, then click Desktop & Dock in the sidebar. (You may need to scroll down.)

Go to Windows, click the “Prefer tabs when opening documents” pop-up menu, then choose an option.

Working with Tomcat in Intellij Idea Community Edition

Is it possible to run a web application using Tomcat Server in Intellij Idea Community Edition?

Yes, by installing Smart Tomcat plugin https://plugins.jetbrains.com/plugin/9492, make following settings. Working with the application and debugging work amazingly well.

What is context? Typically, it is the name of the application.

Build errors for applications in IntelliJ

If you can build the application from terminal but the build is failing in IntelliJ, it indicates that the settings for the application in IntelliJ are not correct. How to fix it?

In the “Maven” panel, select “Edit Run configurations” and look at the Run Configurations. Everything should be default here. Don’t use any custom parameters (like JVM options) here. Clear out any custom parameters that are set.

Where do we set custom JVM options for running applications?

When working with Smart Tomcat,

Run -> Edit Configurations -> Smart Tomcat -> <Your config for your application> -> Environment variables

It will be in the format key=value (No need to put -D at the beginning of the key).

Where are the IDEs located on my hard drive?

Windows: %LocalAppData%\Programs
macOS: ~/Applications
Linux: ~/.local/share/JetBrains/Toolbox/apps

It uses different folders for storing different settings. See https://www.jetbrains.com/help/idea/directories-used-by-the-ide-to-store-settings-caches-plugins-and-logs.html

Where can we find IntelliJ logs

Help -> Show Log in Explorer

TODO

https://stackoverflow.com/questions/32476228/intellij-spring-initializr-not-available

Tags

IntelliJ - Reformatting code before you commit it