About this book

IntelliJ IDEA is a commercial Java development tool which competes with the free Eclipse and NetBeans projects. It is an integrated development environment (IDE) designed to automate parts of the coding process; to support a large number of different frameworks, tools, and targets; to work with multiple languages; and not to get in the way of the developer’s work. IntelliJ IDEA also does a good job of keeping up-to-date with the Java platform, subject to the expense of keeping current with the latest product version.

A practical guide that will provide you with concepts and clear step-by-step exercises to help you understand and improve your performance. Through this guide, you will be able to extract more from IntelliJ IDEA 12.

This book will navigate through the features of IntelliJ IDEA 12 and present the reader with concepts and tips that will give them a greater understanding of this IDE. Using a practical real-world example, readers will learn to develop one application and use some of the features presented. Through clear instructions, you will learn how to integrate the IDE with your favorite bug tracker, take a look at the refactoring approaches that IntelliJ offers, and will quickly understand and use different ways to navigate through the code.

This book teaches you how to develop a web application and a simple Swing application. The development of this application will help you to understand how to work with IntelliJ's Swing GUI constructor. The book will also provide a simple code as starting point and will show you how to develop some code.

Publication date:
October 2013
Publisher
Packt
Pages
114
ISBN
9781849699617

 

Chapter 1. Getting Started with IntelliJ IDEA 12

This introductory chapter will present some features of IntelliJ IDEA (later referred to simply as IntelliJ) and how you can install and configure it. You will be presented with some of the great features of IntelliJ IDEA and will notice for yourself that, even if you've already seen some of the features presented here in other IDEs, none of them are so focused to provide facilities to the work of the developer.

After you've seen the features, you will learn how to install and configure IntelliJ. Despite these being really simple tasks, you will probably see great value in this information, mainly because the organization and nomenclature of some elements may differ from that of the IDE you used before. So, what is provided here is to help you remove some barriers you may encounter while using IntelliJ.

 

Presenting features


As the name suggests, IntelliJ IDEA tries to be an intelligent Integrated Development Environment (IDE). To do this, it applies techniques that permit the automation of some tasks and suggests actions that may facilitate the developer's work. Allied with this, IntelliJ provides out-of-the-box integration with popular frameworks used by Java developers. This native integration drastically reduces the existence of bugs and provides great synergy between the different technologies used to construct your project.

Here, I will show you some of the great features that exist in IntelliJ IDEA 12.

Smart code completion

Code completion is a set of techniques used to analyze and propose pieces of code to the developer. Like other IDE, IntelliJ also presents code completion; but, as compared to other techniques, code completion in IntelliJ is much smarter.

  • It can detect and differentiate between a big quantity of languages, such as Java, Scala (using a plugin), Groovy, SQL, JPQL, JavaScript, and so on, even when they are injected in another sentence as shown in the following screenshot (for example, a SQL string in Java code):

  • Beyond suggested variables, classes, and method names, it can suggest methods, parameters, properties, filenames, resources, Spring Framework's beans, database tables, and so on, even when you are working with non-Java files.

  • Smart code completion suggests sentences based on the context and the user needs. So it can suggest, for example, chain completion like getModule().getProject() that fit correctly in the context as shown in the following screenshot:

  • IntelliJ can propose completion based on middle name search and camel case abbreviation as shown in the following screenshot:

On-the-fly code analysis

An IDE isn't useful if it can't identify whether or not an expression is invalid and will bring compilation errors to the developer. Instead of only identifying these problems, IntelliJ tries to help you improve your code. While typing your source code, you will notice that IntelliJ will analyze your code in the background. In these analyses, IntelliJ will use its configurations to identify errors and possible improvements.

The kind of analyses that IntelliJ does in the code are fully configurable; these configurations are called inspections and are one of the most powerful features of this IDE. With inspections, IntelliJ can find a large range of existing errors, possible bugs, or improvements, such as identifying if a variable is never used, suggesting a simplified sentence, identifying thread problems, and more, as you can see in the following screenshot:

Advanced refactoring

Sometimes, the refactoring process is very tiring because you need to make a lot of modifications to a lot of files. Even when you do the refactoring carefully, sometimes compile errors appear, for example, because you forgot to change a method usage. Fortunately, IntelliJ can help us do these tasks.

IntelliJ has a big range of refactoring options that help the developer refactor his/her code faster and in a more secure manner. Depending on the section of code you are working on, IntelliJ will automatically suggest the refactoring. In case it doesn't propose any refactoring or gives you an option you don't want to use, you can select an option from the refactor menu. The following are some of the refactoring options available:

  • Type migration: With this, the refactoring technique can be useful in changing a member type in a safe way because IntelliJ will find and modify all the usages of the member. You can use this refactoring in a bunch of situations, such as in method return type, local variable, and parameter.

  • Extract class: This refactoring option enables the developer to extract one class from another. The developer can choose which methods and variables he/she wants to transfer to the new class. This is very useful when you have a very huge and complex class and want to simplify it. There is a similar option that creates a superclass based in the current class being edited and another that extracts an interface in the same way.

  • XML-aware dedicated refactoring: There are more than 50 refactoring options exclusive to XML files. For example, you can rename tags and attributes, wrap and unwrap tags, replace attributes with tags and vice versa, convert tags to attributes, safely add and remove subtags and attributes, and so on. If you use some framework that works with XML files, such as Spring, you will see that you can, for example, refactor a bean parameter's name directly in XML.

  • Drag-and-drop items: Sometimes you look at your project tree and think that it would be better to move some classes and packages. In IntelliJ, you can drag-and-drop the elements from one place to another in your project, and it will automatically change the imports and usages for you.

  • Change method signature: You have probably already experienced a situation like this: you created a method and, after doing a lot of work, you've decided to change the method signature. Using IntelliJ, it is possible to minimize the effects of a method signature change. IntelliJ can remove, reorder, and add parameters to the signature; it is also possible to change the return type and the parameter types. In case you are adding a parameter, IntelliJ will insert a default value in all the usages of the method.

  • Introduce constant: Sometimes, while developing, we don't see that it is possible to use a constant instead of repeating the same value all throughout the class. You can easily create a constant based on the repeated values present in your code. Once the constant is created, IntelliJ will detect the entries and replace them as shown in the following screenshot:

Navigation and search

One of the great characteristics you will find in IntelliJ is the ease of finding and navigating to a resource. You can run a search in practically all the controls that exist in the IDE, even when you can't see a search field. You can also use different elements to navigate to a specific code that you want.

The following features can be highlighted to navigate through the source code:

  • A dialog that shows the opened files and IDE windows can be used to open the desired element without using the mouse

  • You can navigate to a declaration from a usage

  • You can navigate to a usage from a declaration

  • A breadcrumb helps to access files that are in the current path or someplace near the actually used file, as shown in the following screenshot:

  • Small symbols are available on the left side of the code editor for navigation. Clicking on these icons will focus the cursor in the desired code as shown in the following screenshot:

  • It is possible to jump to the last change the developer did, no matter if the file is in the current module or closed

For running a search, the following features can be highlighted:

  • You can find the usages of a piece of code by just clicking on one menu

  • Structural searches can be used to find and replace sentences that can't be represented by a simple string pattern, such as finding only the values of field initializers

  • It is possible to find elements in places where you can't see any search field, for example, you can search for a class in the Project Structure tree

  • You can use a regular expression for find and replace

Tools and frameworks support

IntelliJ provides support for different tools that help you to develop your application using it. The integration between the tools and the IDE helps developers to focus and reduces some reworks, mainly because the changes that are made by one tool reflect in another tool and, sometimes, starts a refactoring process. A lot of frameworks have advanced support in IntelliJ; to cite some of them: Spring, Play, Grails, JSF, Struts, Flex, JPA, Hibernate, and so on. The support for these frameworks, in conjunction with the integration of different tools, makes the development very simple and productive.

  • IntelliJ has a Unified Modeling Language (UML) tool that you can use to create your classes or just to see how your classes are organized. The UML tool uses refactoring techniques, so you can change a class in the UML editor and be secure that the code will be changed too.

  • Basically, you won't need to use another program to create databases, tables, keys, and even to store procedures. No matter which database you are using, IntelliJ will guess the correct dialect for the database; in the rare case it doesn't choose well, you can configure the SQL dialect of the project, file, or directory. There is a powerful graphical table editor available, so you can do the majority of your work in a visual environment, as shown in the following screenshot:

  • IntelliJ supports the most known Version Control Systems (VCS) such as Git, subversion, CVS, Mercurial, and more. With this tool the user can work with different VCSs in a similar way. You can view the differences, changes, and history, manage change lists, integrate with your favorite bug tracker, and more.

  • IntelliJ is also fully integrated with the following most commonly used build tools: Maven, Ant, Gradle, and Gant. They provide good integration for unit tests and not only to Java code, but also to JavaScript, Flex, Android, PHP, and others too.

 

What is new


The Version 12 of IntelliJ comes with good enhancements and new features, such as the following:

  • JavaFX 2: Now it is possible to use code completion, navigation, refactoring, and CSS facilities in JavaFX 2 projects

  • Java 8: In this version, the next generation of the Java platform is fully supported with code assistance for the new syntax, such as Lambda expressions, type annotations, default methods, and method references

  • Android UI Designer: IntelliJ provides a full-featured UI Designer for Android; this way, you can build advanced layouts for Android mobile devices by simply dragging elements

  • Spring Frameworks: This framework was already supported, however, new improvements were made that bring performance enhancements and better code assistance for a wider range of Spring Frameworks, such as Security and Batch

  • Flex Flash and AIR: It provides support for Adobe Gaming SDK, including AIR SDK, Starling, Feathers, and Away3D

  • Drools Expert: The rule-based declarative language from JBoss has code assistance and refactoring tools available

  • Cloud Tools: You can deploy, run, and monitor your applications in Cloud Foundry and CloudBees

 

Installing


Now that you know some features of IntelliJ, we need to install it. On IntelliJ's website http://www.jetbrains.com/idea/, you will find two editions of IntelliJ: Community and Ultimate.

As you've imagined, IntelliJ IDEA Community Edition is a free version of the IDE, but, unfortunately, it has lots of limitations. For example, in the Community version you won't have UML and database tools, nor the frameworks' ease for Spring, JPA, and so on, neither will you have support for languages like JavaScript, CSS, and others. You can see the differences between the versions in the following table; notice that all the things the Community Edition supports are also supported by the Ultimate Edition.

Community Edition

Ultimate Edition

Features

Android support

Build tools

Unit testing

Issue tracking integration

Local history

Context management

Eclipse project interoperability

Swing GUI designer

Code spell checker

OSGI

Database tools

UML designer

Code coverage

Dependency structure matrix

Maven dependency diagram

Structural search and replace

Code duplicates detection

Frameworks and technologies

 

Spring Framework

Play Framework

Java EE 6

Google Web Toolkit

Java Persistence API, Hibernate

Struts

Adobe Flex, AIR

Grails

Griffon

Sass, LESS

Rails, RubyMotion

Django

Node.js

Cucumber for Java Groovy

AspectJ

JBoss Seam

Tapestry

Application server

 

Tomcat

TomEE

Glassfish

JBoss

WebLogic

WebSphere

Geronimo

Resin

Jetty

Virgo

Version Control

Git, GitHub

Subversion

Mercurial

CVS

Team Foundation Server

ClearCase

Perforce

Visual SourceSafe

For more details about the features supported by each version, go to http://www.jetbrains.com/idea/features/editions_comparison_matrix.html?IU.

In this book, we will work only with the Ultimate Version 12.1.x. Don't worry, you can download an evaluation version and use it for 30 days. You can run IntelliJ on three different operating systems: Microsoft Windows, GNU Linux, and Apple Mac. Independent of the OS, you will need the following configuration to run IntelliJ:

  • 1 GB RAM minimum, 2 GB RAM recommended

  • 300 MB hard disk space plus at least 1 GB for caches

  • 1024 x 768 minimum screen resolution

  • JDK 6 or higher

You can run it on the following operating systems:

  • Microsoft Windows 8/7/Vista/2003/XP (including 64-bit)

  • Mac OS X 10.5 or higher, up to 10.8 (Mountain Lion)

  • GNU/Linux with GNOME or KDE desktop

The installation of IntelliJ is so simple that it can be done in the following way:

On Windows, the steps are as follows:

  1. Run the downloaded file.

  2. Follow the steps in the wizard.

On Mac, the steps are as follows:

  1. Mount the downloaded .dmg file as another disk.

  2. Copy IntelliJ IDEA to your Applications folder.

On Linux, the steps are as follows:

  1. Unpack the .tar.gz file.

  2. Run idea.sh from the bin subdirectory.

 

Configuring


The first time you open IntelliJ, an initial configuration wizard will be shown to you. This wizard will ask you which technologies you want to enable or disable, then it will pick plugins or a group of dependent plugins. It is recommended that you just enable the plugins that you will use because the more plugins you have enabled, the more time is spent in starting the IDE. Also, you can enable or disable any plugin when you want, in the settings dialog.

The majority of the time, you won't need to configure anything in IntelliJ. For example, if your project uses a Git repository, when you open the project in IntelliJ, it will configure its VCS tool automatically based on the Git's files present in the Git folder. Even if it is necessary to configure something, you will probably see a balloon on the upper-right corner indicating the event and a link which will direct you to the place where you can take action.

Anyway, all configurations are done in the settings dialog. Even when you see options in the context menus or in balloons, they are just shortcuts to sections of the settings dialog. You will also see them in some tabs and buttons used to configure it, but they are shortcuts too.

As you finish the initial configuration, IntelliJ will open the initial window where you can do some actions, such as creating a project or configuring the IDE. In this initial window, you can open the Settings window (or by pressing Ctrl + Alt + S on Windows or command + , on Mac when in the main IntelliJ window). In the Settings window, you will see various items, some of which are specific for the plugins you have enabled, others that are used to configure the entire IDE, and others for project configuration. They are divided in two big sections: Project Settings and IDE Settings.

The Project Settings section is used to change configurations in the IDE that are specific to the current project, such as the compiler, deployment, and VCS. On the other hand, the IDE Settings section is the place where you will manage the configurations that will impact all current and future projects you have, such as Plugins, Notifications, and Keymap. Navigating through the items of the settings window is really simple, but some of them are hidden in a tree structure, which makes it difficult to find them when we need. Fortunately, this can be solved using the search box, as shown in the following screenshot. As I mentioned before, searches are available in practically all IDE elements.

Project Structure

This dialog is used to configure the structure of the project and can be opened by clicking on the Project Structure button present in the main window or using the shortcut Ctrl + Alt + Shift + S (or command + ; in Mac). It's divided into the following five categories:

  • Project: This section is used to configure the general settings for the project, such as project name, the Java version used, the language level, and the path for the compiled output.

  • Modules: This module is a discrete elementary unit of the functionality which you can compile, run, test, and debug independently. In this section, you can configure settings that are specific to each module in the project, such as the dependencies, package's prefix, and paths.

  • Libraries: This section is used to manage the libraries used in the project. Even if the libraries are managed by an external tool, such as Maven, they can be managed in this category.

  • Facets: Facets represent various frameworks, technologies, and languages used in a module. Here, you can manage the facets of the module. This way, you can define if the support for a particular framework is enabled for it (for example, Spring). Once a facet is activated in a module, IntelliJ will provide facilities to work with that facet.

  • Artifacts: An artifact is a draft that defines the layout of the project output. It could be an archive (JAR, WAR, EAR) or a directory. In other words, an artifact defines what the final output of the project will be. In this section, you can configure the properties for the artifact of the project.

The following sections in the chapter are two other categories related to Platform Settings: SDKs and Global Libraries.

Virtual machine options

Perhaps someday you will notice that IntelliJ may be running slow; this may happen, for example, when your project has lots of classes. To avoid this, we could increase the available memory used by IntelliJ. Simply edit the file INTELLIJ_FOLDER/bin/idea.vmoptions (or INTELLIJ_FOLDER \bin\idea.exe.vmoptions on Windows). What you discover upon opening this file are the virtual machine options used to run IntelliJ; so, if you want to change other things related to Java VM, which executes IntelliJ, this is the place to do so.

You should take care while changing or adding options in this file. Depending on the options you use here, the speed of IntelliJ could get worse. For example, setting values very high for the arguments -Xms and -Xmx can make garbage collector decrease its performance. On the other hand, very small values could create an OutOfMemoryError in IntelliJ. You can use the memory usage indicator to help you to configure these arguments. It is localized in the bottom-right corner of the IDE. If the bar indicates that it is using almost the total reserved memory (for example, using 235 MB of 256 MB), it is recommended that you increase the memory reservation argument by at least 500 MB, as shown in the following screenshot:

Migrating from another IDE

If you are a user of another IDE (for example, Netbeans or Eclipse), you will notice that some differences exist in IntelliJ that could make you go crazy when using this IDE.

  • The nomenclature—the name of a feature in another IDE is probably different from that in IntelliJ (for example, project in Eclipse is the same as module in IntelliJ).

  • IntelliJ saves the files automatically, so if you try to close the file to get the old content, it won't work; in this case you can use the History tab.

  • The shortcuts are different. You can change the keymaps (the relation between the shortcut and the action), although there are predefined keymaps for Eclipse and Netbeans users. A better approach is learning from scratch.

  • IntelliJ IDEA compiles files only when it's needed to save system resources for other tasks. IntelliJ is smart enough to know which files have changed, so it recompiles just what is needed. However, if you still want to make IntelliJ compile on every save, you can use the EclipseMode plugin.

  • It doesn't have the deploy-on-save feature. However, you can use the On frame deactivation option available in the dialog or, perhaps, you can use other options, such as the JRebel plugin for IntelliJ.

  • In the other IDEs the user can open multiple projects in one window; in IntelliJ a window is tied with only one project.

The JetBrains website provides complete lists of FAQs. for Netbeans at http://confluence.jetbrains.com/display/IntelliJIDEA/IntelliJ+IDEA+for+NetBeans+Users and Eclipse's users at http://confluence.jetbrains.com/display/IntelliJIDEA/IntelliJ+IDEA+for+Eclipse+Users, which can help you perform the transition to IntelliJ more easily.

 

What to expect in the near future


At the time this book is being written, Jetbrains is developing a new release of IntelliJ. The new version will show improvements mainly in Android development. If you don't know, IntelliJ IDEA is the base of Android Studio, an IDE focused exclusively on Android applications development. The new features and facilities that are present at this time, in Android Studio will be available in IntelliJ IDEA 13, with the addition of other tools and features already present in this IDE. We can highlight some features that will probably be present in the next release:

  • Support for importing the configuration of Android projects from build.gradle and building them through Gradle

  • External annotations (@Nullable, @MagicConstant) for Android APIs

  • Folding of Android string resources in the Java editor to display the actual string value

  • Many new Lint Checks

  • Initial support for editing the RenderScript code

  • Support for working with fragment resources

  • Support for control variations in the palette (for example, multiple variations of TextField with different default settings)

  • Integrated 9-patch editor

  • New UI for viewing the list of connected devices in the Android Tool Window

 

Summary


In this chapter, some features were presented to you just to let you know what you can expect while using IntelliJ. As you've already seen, the tools are totally integrated with the IDE. This will give you the confidence to make the changes you need without worrying about the time you would spend on them. The fact that almost all configurations are centralized in the same dialog, added to the search options available in basically every control, gives you facilities to find what you need in a fast way.

You will probably suffer the first few times you use it; this is because IntelliJ uses a different approach to solve the same problems. However, when you're familiarized with it's way of work, you probably won't want to work any other way.

In the next chapter, you will learn more details about the organization of the IDE and how to explore specific features to improve your development performance.

About the Author

  • Hudson Orsine Assumpção

    Hudson Orsine Assumpção is a Brazilian software engineer with a bachelor’s degree in Information Systems from Universidade Estadual de Montes Claros (Unimontes). He has experience in developing applications in Java EE and ActionScript 3. He also has practical knowledge in Data Warehouse, OLAP tools, and BPMN. He is a certified ITIL V3 foundation professional. Currently, he is developing, with a friend, a web-based ERP system using the Java EE platform.

    Browse publications by this author

Latest Reviews

(2 reviews total)
Great tool - good introduction
The book is short, and I usually like brief introductions, but I found the material was too sparce. I also read the PyCharm book and that was more what I was looking for. It has a little more detail to get you going. This intellij book is not much more than I can get from browsing the doc pages.
Book Title
Access this book, plus 7,500 other titles for FREE
Access now