IntelliJ IDEA
Keyboard shortcuts and esoteric menus
IntelliJ has some odd shortcuts and strange menus, which appear when you don’t want them, but when you do want them, you don’t know what they’re called, or how to find them. Here are some useful ones:
- Select In: Alt+Shift+1. To quickly select the current element in any view (Project view, Maven view, etc.)
- Quick Documentation: Ctrl+Q. To view documentation for whatever object (class, type, etc.) currently at the caret.
- Quick Switch View Scheme: Ctrl+`. Show popup to change theme, zoom, and more. See https://www.jetbrains.com/help/idea/ide-viewing-modes.html
Installation and upgrade
To install:
curl -o intellij-idea.tar.gz -L https://download.jetbrains.com/idea/ideaIC-2020.1.tar.gz
mkdir -p /opt/idea
tar -C /opt/idea -xvf intellij-idea.tar.gz
Adding to GNOME Applications menu
An example entry for the GNOME Applications menu - note the reference to the startup script, idea.sh:
$ cat ~/.local/share/applications/jetbrains-idea-ce.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=IntelliJ IDEA Community Edition
Icon=/opt/idea/idea-IC-201.6668.121/bin/idea.png
Exec="/opt/idea/idea-IC-201.6668.121/bin/idea.sh" %f
Comment=Capable and Ergonomic IDE for JVM
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-idea-ce
Adding a symlink
To add a symlink (so you can easily start IntelliJ from the terminal on Linux):
$ sudo ln -s $IDEA_HOME/bin/idea.sh /usr/local/bin/idea
# Then you can run using:
# cd myproject && nohup idea . &
Logs
To view log files, go to Help - Show Log in Files (or Finder, depending on platform)
- e.g. on Linux, my log files are in
~/.cache/JetBrains/IdeaIC...
IntelliJ project files
Removing all IntelliJ project files:
find . -name '.idea' -exec rm -r "{}" \;
find . -name '*.iml' -type f -delete
Working with version control systems
Resolving conflicts
Useful process for larger projects:
- Open the Version Control window using View > Tool Windows > Version Control.
- Click Local Changes to show local changes (which will include conflicts)
- Click the gear icon and Group By either Directory or Module (makes it easier to just work on merge conflicts in a specific directory or module)
Debugging
Debugging unit tests using IntelliJ
- Create a new Run Configuration in IntelliJ.
- Set the command line property to your test Maven goal name, e.g.
test - On the Runner tab, set VM Options to:
-DforkMode=never - Set any breakpoints in your code
- Now Debug your Run Configuration, e.g. Ctrl+D, or Run → Debug ‘my-app [test]’.
Debugging an external application
- Choose Run → Edit Configurations.
- Add a new Configuration of type Remote. Name it debugging.
- Copy the command line arguments for running remote JVM (e.g.
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005) - (Optional) Modify
suspend=yto make the Java application wait until the debugger is connected before starting. - (Optional) Set the debug flags in the environment variable
JAVA_OPTS. - In a Terminal, start the application to be debugged.
- In IntelliJ, start the remote debugger (Run → Debug → debugging).
For example:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 -jar target/spring-boot-camel-xa-1.0-SNAPSHOT.jar
Debugging a Maven execution
-
Create a Run Configuration type “Remove JVM Debug”, setting the debugger’s mode as “Attach to remote JVM”, with listen port of 8000.
-
Click OK.
-
In a terminal, run Maven using
mvnDebug goal1 goal2 -
Maven will wait on port 8000 for a debugger to be connected.
-
In IntelliJ, go to Run → Debug… and then select the debugger Run Configuration you just created.
Debugging for specific tools/frameworks
Spring Boot 2.x
Since Spring Boot Maven Plugin now forks the Java process since version 2.2, debugging breakpoints won’t stop/work when running the application using the spring-boot:run goal.
Instead, run the application using the bootstrap class: find the @SpringBootApplication class and just run it from the IDE (green Play button).
Thorntail Maven Plugin
Set the property thorntail.debug.port which causes Thorntail Maven Plugin to suspend on start and open a debugger on this port (when using run or start goals), e.g.:
$ mvn thorntail:run -Dthorntail.debug.port=8000
Troubleshooting
IntelliJ tries to build the entire project before running a single test
- Run → Edit Configurations
- Navigate to JUnit → Your project, and then remove Make from the list of ‘before launch’ tasks.
Cannot resolve classes/objects/dependencies (lots of squiggly red underlines everywhere)
- Open the IntelliJ logs (see location above) and look for any Maven errors - logger name will be something like
#org.jetbrains.idea.maven:- e.g. Maven failed to find an artifact, but cached this failure and won’t try again: “java.lang.RuntimeException: org.eclipse.aether.transfer.ArtifactTransferException: Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.4.1 was cached in the local repository“
- Check that the project has been added as a Maven project (right-click the pom.xml → Add as Maven Project)
- Ensure that IntelliJ is using the correct Maven binary - go to Settings → Maven, and check the path to the Maven installation location, and
settings.xml. - Check that artifacts can be resolved correctly. Drop to a Terminal and, using the same Maven binary configured in IntelliJ, try
mvn -U help:effective-pom. If artifacts can’t be resolved here, then fix this first. - If you’re pulling artifacts from a Nexus repository, check that it’s able to pull artifacts from the web. Log in to Nexus and check the status of your proxy repositories.
- Check that
maven-compiler-pluginis configured correctly (e.g. with a version number) - Project wasn’t imported correctly the first time round => Run the action Reimport All Maven Projects (either from the Maven “tool window” or from the Actions popup (Ctrl+Shift+A))
- Dependencies weren’t imported, and Maven cached the failure and refuses to try again => Go to Settings - Maven and make sure “Always update snapshots” is ticked (despite the label saying ‘snpashots’ on the checkbox, this actually seemed to fix an issue when Maven refused to re-attempt to download a missing dependency.)
- You’re trying to add a Java project in a subdirectory, in a monorepo project, but the root (top-level) is configured with the wrong SDK.
- Right-click the sub-project (“module”) and open settings (F4).
- In Project Settings > Project, ensure that the top-level project is configured with the SDK you want.
- Or, to override the SDK for a particular module, go to Project Settings > Modules, find the module you added, and on the Dependencies tab, set the Module SDK.
Package names aren’t resolving (classes can’t see other classes, etc.)
- Check that the
mainandtestfolders have been added as Sources - Right-click the
src/main/javafolder, right-click → Mark Directory as → Sources Root - Right-click the
src/test/javafolder, right-click → Mark Directory as → Test Sources Root
XML Schema URLs are appearing in red in the XML editor
- IntelliJ doesn’t know how to resolve the XML schema definition (XSD)
- If one of your Maven dependencies contains the XSD file already inside the JAR, then IntelliJ will use that.
- The red error highlighting indicates you either need to add a Maven dependency which contains the missing XSD, or you need to download the resource manually.
- Ctrl+hover over an XSD which is valid, and you’ll see the path that IntelliJ has resolved to the XSD file (e.g. if it’s been resolved from a Maven dependency, it’s a
$HOME/.m2/repositorypath) - Hover around the red text and wait for the Intention (light-bulb icon) and choose the command Fetch external resource
- See also: Map External Resource feature - which allows you to map a URI to a file inside a JAR manually.
- See also: Settings -> Resources where you can map URIs to physical files on disk manually.
External Libraries list is empty, but you expect there to be entries of dependencies, etc.
- Could be something to do with path setups. Check Sources, Paths, Dependencies (Project Settings → Modules)
- Try deleting the IntelliJ project and opening the project again.
Issues with building and running under different JDK versions
- Check Settings → Build Execution Deployment → Compiler → Java Compiler. Check that strange JDK version-specific settings haven’t ended up in here.
Cannot load a Go project in a subfolder
Ensure that Go Modules are enabled:
- Settings -> Languages & Frameworks -> Go -> Go Modules. Ensure Enable Go modules integration is checked.