(Former) IntelliJ Notes

Russell Bateman
February 2014
last update:

Notes I make as I'm forced to use this IDE. I know others think it's wonderful, but I'm an Eclipse guy and see no reason to change over were it not for my new employer forcing me to.


Setting up IntelliJ

What's crucial is:

  1. Ensure Maven set-up is working. IntelliJ relies on Maven.
  2. Ensure that JAVA_ROOT points to a JDK (and not merely a JRE). What signals the difference is that ${JAVA_HOME}/lib contains tools.jar. If not, then it's a JRE not a JDK. IntelliJ will not run without this JAR and will tell you so.

Structure in IntelliJ—Outline in Eclipse

What's called Outline in Eclipse is a side-ways control in the left margin in IntelliJ. The way it works actually makes sense and leads to something to think about in the way I lay out my Eclipse panes.

Get Outline details of current file: Try adding Outline view to left-most pane (Package/Project Explorer views) à la IntelliJ. This may work better because when the current file's outline interests you, no other files do so you can sacrifice the Package, Project or Navigator views for the time you're doing that.


Miscellaneous stuff in IntelliJ

To bring up a heretofore unused project from the filesystem into IntelliJ, File -> Open -> browse for pom.xml in the new project.

Keys...

Key Description of function Eclipse equivalent
Ctrl N brings up search dialog to look for resources, classnames, etc. Shift Ctrl R/T
Ctrl Alt B Pop-up list of implementations of the interface under cursor. ?
Ctrl Y Deletes the current line. Ctrl D
Autoscroll from/to Source under cogwheel in Project title bar. (icon in Project Explorer top bar)
Show diff just go to the filesystem and use git diff.
Shift-Ctrl-F Find all throughout project (via menu Eit → Find → Find in Path Ctrl-H

Configuring keyboard shortcuts

Too long used to Ctrl-D for "delete line", I had to remap it in IntelliJ which prefers Ctrl-Y and uses Ctrl-D for "duplicate line" (an action I don't use). Despite this, it's a good example for any other re-mapping you want to make.

  1. File -> Settings....
  2. Look down the left pane for IDE Settings -> Keymap.
  3. Click Copy; name the new Keymaps; I called mine Russ.
  4. Look for Delete Line which is mapped to Ctrl-Y.
  5. Right-click and choose Remove Ctrl-Y; this will stop IntelliJ from continuing to use Ctrl-Y for deleting a line.
  6. Right-click again and choose Add Keyboard Shortcut.
  7. Type Ctrl-D.
  8.  
    In order for this to work now, you must also do the following steps:
  9. Look for Duplicate Line or Block which is mapped to Ctrl-D.
  10. Right-click and choose Remove Ctrl-D; this will stop IntelliJ from continuing to use Ctrl-D for duplicating a line or block of text.
  11. Do not map Duplicate Line or Block to anything (unless you want to).

Other keyboard shortcuts to rewire include Last Edit Location, Eclipse uses Alt+Left, Next Edit Location, Eclipse uses Alt+Right.

Here are some mappings to contemplate.


Turn on line numbers and off vertical guides

Here's how to turn line numbers on permanently and remove vertical indent guides (those annoying vertical lines in the editor).

  1. File -> Settings....
  2. Look down the left pane for IDE Settings -> Editor.
  3. Look for Appearance.

How to invoke diff for a file
  1. Look to bottom of workbench, click on Changes.
  2. Right-click on Default or the file you wish to see.
  3. Choose Show Diff.

A missing class...

Symptom

A class is missing even when the import statement is typed in by hand (and valid).

Analysis

The JAR containing the class is missing (duh).

Solution

The JAR must be included in the module's pom.xml. The JAR may be known to other modules in the project. Here's how to add it to the current module where it's missing:

  1. Figure out which JAR supplies the missing symbol:
    1. Right-click the project root—that is, the top-level project (and not the module missing the symbol).
    2. Choose Find in Path and narro the search to pom.xml files.
    3. Choose a good occurrence in the resulting search output list.
    4. Copy the whole dependency.
  2. Open pom.xml (in the module missing the JAR/symbol) and paste in the new dependency clause.
  3. Open Maven Projects view (right-hand margin of IntelliJ workbench) and click the reload button (circling arrows at upper-left of view).

Note that a module may not be missing the symbol and yet not define the dependency because it inherits dependencies from the project (root) pom.xml as long as it defines none of its own.


Solving the nasty propensity to collapse import lists into wildcards

IntelliJ loves to corrupt import statements by collapsing like package names down and discarding all the classnames in favor of using a wildcard (*). To turn this off, I apparently had to set the number of imports up to 99 in:

File -> Settings -> Code Style -> Java -> Class count to use import with '*'
                                                      Names count to use static import with '*'

Method returns in Java code

Hovering over a method will not, as in Eclipse, tell you anything about it. Instead, holding down the Ctrl key and hovering will tell you or hovering and pressing Ctrl-Q will get you the Javadoc.



Attaching source code

Here's how to attach sources in IntelliJ. This is one thing that's easier and more successful in IntelliJ than in Eclipse.

  1. Click link Attach Sources.
  2. In the Attach Sources dialog, navigate to the root of the project containing the source code, e.g.: project ct.
  3. Click OK.
  4. See "Scanning for roots"; choose a root or, for all source code, click OK (since all are selected).
  5. Enjoy population of editor window with source code.

Sorting out Eclipse problems in code from IntelliJ

Actually, Maven-organized code from IntelliJ. This is about the pains of trying to keep Eclipse going on the side.

Bad title here, but this was one of the main problems I ran up against when trying to get some code working in Eclipse. All the warnings I was able to sort out using Window -> Preferences -> Java -> Compiler -> Errors/Warnings.

Below I seem to waffle on whether it's one project or many. This is because I'm trying to write generically, but in fact, I'm working on what is multiple modules in a hierarchy in IntelliJ. In Eclipse, they are simply sibling projects (how they also appear in IntelliJ).

Steps

  1. Create a clean Eclipse workspace.
  2.  
  3. In (main) project, tell Maven to create Eclipse project(s). Make sure everything Maven is up to date, project(s) is(are) clean and up to date, etc. If your project is really hierarchical and subsumes additional projects, and the Maven pom.xml files have been well set up, then this command will suffice for all if executed from the main project root (acme-parent):
    ~/dev/acme-parent $ mvn eclipse:eclipse
    
  4. Import project(s) into Eclipse using

    File -> Import... -> General -> Existing Projects into Workspace -> Next -> Browse

    Select the project (root project) and click OK. You should see one or more projects; select them all and click Finish.

  5. Errors

    These will be of several sorts. First, the unimportant ones that stem from the habits of whoever wrote the code. You'll need to go into Window -> Preferences -> Java -> Compiler -> Errors/Warnings and turn off (Ignore) any errors or warnings you don't wish to face. Alernatively, you could clean up the code itself.

    Important errors and warnings

    These are what are truly challenging to fix. They're what's covered in the remaining steps of this section.

    Before continuing, ensure you have no m2e (Eclipse-Maven integration) plug-ins installed. The reason for this is because this particular solution does not seek to use those. If you wish to build your project using Maven, you can always do so via the command line. This solution seeks to fix Eclipse build errors, a sampling of which I've encountered follow.

    Fix errors in Eclipse like...

    4bis


    Because IntelliJ is so Maven-oriented, things that are caused by or incidental to Maven usage appear to be problems I'm covering here. To fix the errors in the illustration above, set the classpath variables in the Eclipse workspace as shown in the illustration below (see, in particular, M2_REPO):


    4ter

    Here's another error:

    Project 'acme-client' is missing required source folder: '/home/russ/dev/acme-parent/acme-config/environments/hudson'
        Build Path Problem
    

    by doing this:

    1. Right-click on the project experiencing the problem (in this case, acme-client, choose Properties.
    2. Click Java Build Path.
    3. Click on Source tab.
    4. Look for a source folder in the list that matches the name in the error; this will likely be marked with "(missing)".
    5. Select the line with "(missing)" on it and click Remove button. (See illustration below.)
    6. Click OK; likely the Errors list will be shortened by at least one line.

    There is some obfuscation in this illustration because I'm protecting my employer's source and reputation.

  6.  
  7. Fix missing symbol, method, etc. errors. This is where being crippled in Maven integration has its effect. I found that by introducing Eclipse-Maven integration plug-ins, I was only creating insurmountable headaches for myself. (I tried this in the latest Eclipse Kepler release and also Indigo.)

    I found that refreshing ~/.m2/repository has no effect on workspace projects when they begin to require updated JARs coming from the Maven repository, so compilation errors like missing symbols go unsolved. I've tried bouncing Eclipse, tried installing m2e and more m2e stuff (some of which Eclipse moans about), but I can't get it to see the new stuff in the local repository.

    What did work was rebuilding my workspace following the steps above. Of course, this is not helpful. It might be that the affected project(s) need special treatment. I will come back and post a solution to this as soon as I find one. It's probably going to be simple.

    Stuff to try:

    • Bounce workbench—didn't work.
    • Do Maven build on command line.
    • Do build using IntelliJ (in parallel).