2007-01-16 LiTH
This document is a short introduction to the Jetbrains [Jetbrains] product IntelliJ IDEA which is an editor for Java developers. It shows how some basic tasks is done in the editor and then it presents some more advanced features which can be used to improve the project at hand. In the beginning there is also a short discussion how IntelliJ
IDEA compares against other editors. At the end the reader should have a display of what IntelliJ IDEA can do for him.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
1
2
Field of application
One of the advantages with the Java platform is that there are tons of editors out there that claim to make life easier for the developer. Jetbrains IntelliJ
IDEA is one of the better ones. This process was created in order to introduce
IntelliJ IDEA as one of the options for Java development. This document will make a short presentation on why IntelliJ IDEA might be good decision. It will describe how to get started with a J2SE [Sun] project and describe how to use some of the key features.
This document assumes that the reader know the basics of the Java language [Sun].
The start configuration is Jetbrains IntelliJ 6.0, which can be downloaded from Jetbrains website . The user also needs to have an installed copy of JDK
5.0, which can be downloaded from Sun's homepage [SUN]. Newer versions might also work.
This section describes the realization of the process. We start with a short description of IntelliJ IDEA and why it might be better than other editors, then a tutorial on how to create a basic project and then we end by showing of some key features.
The readers of this process probably know about other editors that can be used for development with Java. While the amount of editors is a big number the main competitors are probably Eclipse [Eclipse] and NetBeans [Net-
Beans]. The functions in IntelliJ IDEA is very similar to both Eclipse and
NetBeans. That is, nowadays they all got GUI designers, JUnit [JUnit] integration and good content handling support and so on. In IDEA this is integrated from the start and you don't have to download and install a lot of extra plug-ins that sometimes isn't of the greatest quality.
Disadvantages
The main disadvantage with IntelliJ IDEA is that it is a commercial product and it cost a bit to acquire a license while Eclipse and NetBeans are free.
However it is possible to download a 30-day trail which is enough to test the editor. Compared to Eclipse the performance of IDEA is good when it comes to algorithms but much worse on the graphical side since it doesn't use platform native widgets. IDEA will require a somewhat decent computer - thin clients are a no go. IDEA is better when it comes to prompting for alternatives and mistakes in your code.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
Advantages
If the license isn't a problem the main advantage with IntelliJ IDEA is that it offers easy to use functions with a very good quality compared to the Eclipse editor and other editors. Sometimes other editors strive to bring more functionality into the tool by introducing support for another languages, like the
C++ language, which usually means that new functionality for Java is ignored or delayed until another version gets out. IntelliJ IDEA got a big set of awards from different Java communities on the net which, in some sense, confirms how good it is. IntelliJ IDEA completely concentrates on Java developement in all forms and can be used in combination with J2SE, J2EE and for web developement. Web developement got support for EJB 3.0 and several frameworks such as Struts and GWT. Key features in web developement aims to make JavaScript, AJAX, HTML/CSS, XML and JSP easier to use.
Other features that aim to make life easier is an integrated Swing Editor,
JUnit/Code coverage integration, refaction, code analysis and team work.
We are now going to show you how easy it is to create and configure the environment of a new project in IntelliJ IDEA.
Creating a new project
We will start with creating a project named "demo" so start by starting IntelliJ IDEA.
You will now find yourself at the main screen. To create a new project press on the button "Create New Project"
, see Figur 1., or use the meny alternative
File->New Project…
Figur 1.
Button for creating a new project.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
3
Realization
You will then be prompted to enter a name for your new project. Enter
"demo" and then select a source path for the project, see Figure 2.
Figure 2.
Naming your project.
IDEA will then prompt you to enter a destination for the compiled classes,
see Figure 3. It is usually wise to have these in the project folder so lets just
create a path named classes in your existing project path.
Figure 3.
Specify output path.
It is now time to specify which JDK you want to use. You can select one from the list (if you have one added) or you can choose to add a new JDK. A new
JDK is added by pressing the [+] button, see Figure 4.
Figure 4.
Add a JDK to the project settings.
4 RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
Now you can also add other paths to this JDK. One good idea is to add a path to the JavaDoc. So download the latest JavaDoc from SUN, select the path
tab and add it to the paths.See Figure 5.
Figure 5.
Adding and viewing classpaths.
Continue by pressing " Next " and go with the Single Module alternative on the next page. One module is usually quite enough and we will not cover anything else in this brief document.
IDEA will now prompt for what type of module it should create. If you are developing a J2SE application you should go with the default " Java Module " al-
Figure 6.
Select module type.
Press “ Next ”.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
5
Realization
You will now get the alternative to name your module and specify where the
files that the module accesses are stored, see Figure 7.Do this now.
Figure 7.
Naming your module in the project.
Press “ Next ”.
Specify the path to the source, see Figure 8. This is usually already done so if
you don't see any need to change this default value go to the next screen.
Note that it is always a good idea to create a source folder. Do not select to ignore this option.
Figure 8.
Specify how to create a source directory.
We are done.
Changing JDK or Library
Changing JDK is sometimes of interests - especially if you want to find out how much better a newer JDK does a certain task or maybe you just want to check capability between two different versions. In intelliJ IDEA this can be done by simple changing in the preferences panel.
File->Settings .
Select " Project Structure ", see figure 9.
6
Figure 9.
The Project Structure button.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
You can now add JDK and the libraries that you want. To add a new JDK go to the "Project" and select your module. Go to the "Dependencies" tab and se-
lect new, see Figure 10. The procedure there after is similar to what we did
before.
Figure 10.
JDK and Libraries in your project and modules.
Run your project
After we have added some code, which is considered to be trivial - just right click and add a class in your tree view. We will obviously want to run it sooner or later. (Make certain that you got the main method defined.) Add a " New
Configuration
…" by pressing on the selected area showed in Figure 11.
Figure 11.
Edit Configurations.
Tip: in IDEA a fast way to add a main method is to type " psvm " and hit
This is called live templates and we will get to it later.
Tab .
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
7
Realization
We will now get a huge set of predefined configurations that we can set, see
Figure 12. To run a basic project we should pick “
Application ”. Note that we can add Applets, J2ME applications as well as JUnit tests.
Figure 12.
Add new Configuration.
8 RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
Enter a name for the Configuration and specify target class. We can also add parameters and/or run the application with a different JRE, but we will not do that now. Before lunching IDEA will build the application. If we work on a slow machine this might not be advisable but if we can run IDEA it's probably not a problem so let it be.
Figure 13.
New application that you can run and debug.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
9
Realization
We can now run our " Application " by selecting it from the quick drop down
menu and press run, see Figure 14.
Figure 14.
Button for running selected application.
Debug your project
To debug your project you simple run it in debug mod. The simplest way to do
that is to press the icon next to the run button, see Figure 15.
Figure 15.
Button for debug selected application.
To add a breakpoint, which can be either a method- or a line-break, left click
out in the left border - just next to the code, see Figure 16.
Figure 16.
Examaple on how break points will look like and how to place them.
10 RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
When you debug your application you will be able to get plenty of feedback.
One thing that is nice is that it’s simple to monitor threads, see Figure 17. In
other editors this isn't always an option or it's difficult to get an overview. It's also one of the advantages with the Java language, because it got pretty abstract and easy to use thread handling rutines.
You can also monitor the values in the current Frame and add Watches.
Figure 17.
Monitor your threads in the debug mode.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
11
Realization
JavaDoc generation
At the end of your project you probably want to have documentation and a smooth way to deploy your application. IntelliJ IDEA uses several templates that can (and should!) be edited in order to make JavaDoc easier to automat generate the documentation. When you add a comment to a method (for instance by typing " /** … **/ ") IDEA will automatically add parameters and return comments in your commented section which you then simple fill in.
While it's not perfect it's by experience more functional than what you find in most tools. In combination with a code standard, that you set up for your team, it can be a very powerful tool because used right it is an easy and fast way to add quality to your work.
To generate a Javadoc of your module you go to Tools-> Generate JavaDoc… .
Here you will be promteded to specify some settings for the document. The
defaults are usually good enough, see Figure 18.
12
Figure 18.
JavaDoc generation options.
Make certain to select an output directory and press " Start ". The new Javadoc will open in a browser window when it is finished.
Tip: It can be a good idea to use a temp directory so you can toy with the setting a bit.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
Deployment
IntelliJ IDEA integrates with ANT so if you want to be able to easily build your application on another system you can simple export it as an ANT project with corresponding makefile. If you just want to distribute it you can do so by letting IDEA creating a JAR that you then can share (maybe by using WebStart).
To deploy as an ANT make:
It's easy to make an ANT build project. Go to Build->Generate Ant Build… .
The default settings are probably good enough. It will look something like
Figure 19.
Generate ANT build options.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
13
Realization
IDEA will give you a popup notice when it is done.
To deploy as a JAR:
To create a JAR file you do something pretty similar. Go to Build->Build
Jars . Select the modules you want to include and select the main class, see
14
Figure 20.
Generating a JAR build options.
IDEA will now build a JAR and save it in the given path.
In this section we introduce some of the common tools and techniques that you can access in IntelliJ IDEA. Some of them are more or less identical to functions that you can find in other editors, such as Eclipse.
Keyboard shortcuts and templates
Keyboard shortcuts is nothing special. Most editors use them. In IntelliJ
IDEA they are however essential since they allow the developer to automate generate code with haste by using predefined templates. It's also possible to add your own templates.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
IDEA uses something called live templates which let you define a template and then make it easy to apply when you are writing your code. Things that
IDEA does with haste are to generate code blocks such as different types of iterations. We have already used one of these default templates, namely "psvm".
To use a template you simple type the tag followed by a tab.
Live Templates can be viewed and changed in Settings->Live templates , see
Figure 21.
The default live templates (just to give it a brief look).
Code analysis
Code analysis is another function in IntelliJ IDEA that can be good to use in order to improve the quality of your work. While it doesn't always lead to improvement it does offer some insight and sometimes it can find really good solutions that you might have missed. Simple put, it could be seen as a poor mans alternative to real code inspections.
To do a code analysis go to Analyze->Inspect Code… and you will get a dialog with options. Here you can select the scope of the analysis which is good if
you have big projects, see Figure 22.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
15
Realization
Figure 22.
Code inspection options.
The result will display common code mistakes. In this example we have created an int but we haven't given it any initial value and we don’t use it so the code inspector will complain. This is just to show how it will look like after-
16
Figure 23.
Example output from a code inspection.
Test integration - JUnit
Testing your code does almost always lead to improvement. If not, it does at least provide a tool for checking whatever your code does what you expect it to do before you pass it on to more serious tests and waste precious time.
JUnit is a library that has gained ground these past years in the Java world.
It provides functionality to make test cases and then run hem. JUnit [JUnit] is integrated into IntelliJ IDEA which makes this even easier and faster.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Realization
After having added JUnit to your project it's a trivial task to add a test case.
Just do the same as when we previously created a new configuration but instead of adding an "Application" we will add a JUnit test. It will look some-
Figure 24.
Adding a new JUnit test configuration.
Just note that the JUnit library must be added to the module.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
17
Results
So how productive are you?
One thing that is nice is IntelliJ IDEA is that it got a tool that helps you check on what functionality you have been using. To view it go to Help->Productivity Guide…
You will then get something like this, see Figure 25.
18
Figure 25.
The productivity guide for this project.
Now you can browse and see how you can increase your productivity with IntelliJ IDEA.
Jetbrains got tons of information on the homepage of IntelliJ IDEA.
http://www.jetbrains.com/idea/
The reader should be able to set up a basic project. Do some basic configurations and use some of the main tools in IntelliJ IDEA.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
Templates and forms
The reader will learn the basics of IntelliJ IDEA and hopefully expand from there.
None at the moment.
None at the moment.
The reader should be able to set up a basic project and use some of the basic tools.
None at the moment.
None at the moment.
This RUT is usable both in the PUM-course and maybe in other situations as well. One special adaptation is that it aims to describe and give a wide idea of the tool by using a lot of figures rather than going into details on how you actually do something more specific.
None at the moment.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
19
History of the process description
0.1
0.2
Version
1.0
Date
2007-01-13
2007-01-15
2007-01-16
Kommentar
First version.
Removed a basic section about refactoring.
Added a section about templates instead.
Fixed the layout a bit so it’s more grouped with figures.
Författare
Rickard Jonsson
Rickard Jonsson
Rickard Jonsson
This RUT is pretty easy to expand. There are several idea on how to do this.
The most obvious one that should come to mind is to use the tool in a PUMproject and fill in the headings that currently doesn’t contain anything based from the experiance of that project. Another idea is to expand it and make it more refined. Either by editing it to the better or add more content.
Here are some other ideas:
■
■
■
■
■
Expand and go more into details. One idea could be to add a more information about JUnit integration and how to set it up.
Find workflows that aids the tool and development. IntelliJ IDEA uses
Jetbrains new product " TeamCity " [Jetbrains] that let the users communicate. Explain how this works by setting it up and investigate how it aids your development process.
Add more descriptions of the tools available and expand it. This can be done by, for instance, add content management support and explain how to set it up with CVS or Subversion.
Add custom made templates that don't exist and explain how to do this in detail.
Expand how you do J2EE web development with IntelliJ IDEA.
20 RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
References
[Jetbrains] Jetbrains, IntelliJ IDEA, http://www.netbrains.com, 2007.
[Sun] Sun, Java language, http://www.sun.com/java, 2007.
[Eclipse] Eclipse, The Eclipse project, http://www.eclipse.org, 2007.
[Netbeans] Netbeans, http://www.netbeans.org, 2007.
[JUnit] JUnit, http://www.junit.org, 2007.
RUT - development handbook 9.96 Introduction to IntelliJ IDEA v 1.0
21