Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | kartoonist360 |
View: | 224 times |
Download: | 0 times |
of 20
8/14/2019 Creating and Running Your First Java Application
1/20
Creating and running your first Java
application
Current revision(unreviewed)To get an impression of how IntelliJ IDEA can help you develop and run
Java applications, we suggest that you create, build and run the age-old
Hello, World example. By doing so, youll be able to learn about the IDE
features without the need to concentrate on coding.
Contents[hide]
! 1 Before you start! 2 Creating a project
2.1 Exploring the project
structure
! 3 Creating a package! 4 Creating a class! 5 Writing code for the HelloWorld
class
5.1 Using a live template forthe main() method
5.2 Using code completion
5.3 Using a live template forprintln()
! 6 Building the project! 7 Running the applicationBefore you start
To develop Java applications, you need a Java Development Kit (JDK).So, prior to performing the exercises discussed in this tutorial, make sure
that you have a JDK installed.
If necessary, you can download an Oracle JDK from the Java SE
Downloads page. If you need installation instructions, you can find them
there as well.
Creating a project
Any new development in IntelliJ IDEA starts with creating a project. (To
find out why a project is needed, see Projectin IntelliJ IDEA Help.)
For the purposes of this tutorial, we will create our new project from scratch
and give it a name HelloWorld. We will also create a Java module with the
8/14/2019 Creating and Running Your First Java Application
2/20
same name (HelloWorld) and a folder for our source code called src.
1.To start creating a project, select New Projectfrom the Filemenu.
As a result, the New Projectwizard opens.
2.Select Create project from scratch.
8/14/2019 Creating and Running Your First Java Application
3/20
Click Next.
3.The page of the wizard that you see now is for specifying the main
project settings.
In the Project namefield, type HelloWorld. The rest of the settings are
OK and dont need to be changed. However, make sure that:
The Create moduleoption is selected.
The module name is the same as the name of the project (HelloWorld).
(This name is shown in the Module namefield.)
Java Moduleis selected under Select type.
Click Next.
8/14/2019 Creating and Running Your First Java Application
4/20
4.On this page of the wizard, accept the default option Create source
directoryand the default directory location and name src.
Click Next.
5.If you have never configured a JSDK in IntelliJ IDEA, you should do itnow. (If the page discussed below is not shown, you already have a JSDK
configured. In this case, proceed to completing the wizard.)
8/14/2019 Creating and Running Your First Java Application
5/20
Click Configure.
In the Select Home Directory for JSDKdialog that opens, select the
directory in which you have the desired JSDK installed, and click OK.
The JSDK you have just specified is shown and selected under Project
SDK.
8/14/2019 Creating and Running Your First Java Application
6/20
Click Next.
Note that the specified JSDK will be associated by default with all projects
and Java modules that you will create later.
6.The last page of the wizard is for specifying additional technologies to besupported in your module.
8/14/2019 Creating and Running Your First Java Application
7/20
Because our application is going to be a plain old Java application, we
dont need any of those technologies. So just click Finish.
Wait while IntelliJ IDEA is creating the necessary project structures. When
this process is complete, you can see the structure of your new project in
the Projecttool window.
Exploring the project structure
Lets take a quick look at the project structure.
8/14/2019 Creating and Running Your First Java Application
8/20
There are two top-level nodes in this structure:
! HelloWorld.This node represents your Java module. The .ideafolderand the file HelloWorld.imlinside are used to store configuration
data for your project and module respectively. The folder srcis for
your source code.
! External Libraries.This is a category that represents all the externalresources necessary for your development work. Currently in this
category are the .jarfiles that make up your JDK.
Out of all the folders mentioned, srcis the only folder that well use in our
exercises.
(For more information on tool windows in general and the Projecttool
window in particular, see IntelliJ IDEA Tool Windowsand Project Tool
Windowin IntelliJ IDEA Help.)
Creating a packageNow we are going to create a package for the HelloWorldclass. (Well
create this class a bit later.) Let the package name be
com.example.helloworld .
1.In the Projecttool window, select the srcfolder and press
ALT+INSERT. (Alternatively, you can select File | New, or Newfrom the
8/14/2019 Creating and Running Your First Java Application
9/20
context menu for the folder src.)
2.In the Newmenu, select Package. (Use the UP and DOWN arrow keys
for moving within the menu, ENTER for selecting a highlighted element.)
3.In the New Packagedialog that opens, type the package name
(com.example.helloworld). Click OK(alternatively, press ENTER).
The new package is shown and selected in the Projecttool window.
8/14/2019 Creating and Running Your First Java Application
10/20
Creating a class
1.Press ALT+INSERT. The Newmenu is shown; the Java Classoption is
currently highlighted. Press ENTER to select it.
2.In the Create New Classdialog that opens, type HelloWorldin theNamefield. The Classoption selected in the Kindlist is OK for creating a
class. Press ENTER to create the class and close the dialog.
8/14/2019 Creating and Running Your First Java Application
11/20
The HelloWorldclass is shown in the Projecttool window.
At the same time, the file HelloWorld.java(corresponding to this class) is
opened in the editor.
Note the package statement at the beginning of the file and also the class
declaration. When creating the class, IntelliJ IDEA used a file template for
a Java class. (IntelliJ IDEA provides a number of predefined file templates
for creating various file types. For more information, see File TemplatesinIntelliJ IDEA Help.)
Also note a yellow light bulb . This bulb indicates that IntelliJ IDEA has
suggestions for the current context. Click the light bulb, or press
ALT+ENTER to see the suggestion list.
8/14/2019 Creating and Running Your First Java Application
12/20
At the moment, we are not going to perform any of the actions suggested
by IntelliJ IDEA (such actions are called intention actions, see IntentionActionsin IntelliJ IDEA Help.) Note, however, that this IntelliJ IDEA feature
may sometimes be very useful.
Finally, there are code folding markers next to the commented code block (
and ). Click one of them to collapse the corresponding block: we dont
really want to see this code block at the moment. (You can as well place
the cursor within the code block, and then use CTRL+NumPad- or
CTRL+NumPad+ to collapse or expand the block. For more information,
see Code Foldingin IntelliJ IDEA Help.)
Writing code for the HelloWorld class
The code in its final state (as you probably know) will look this way:
packagecom.example.helloworld;
public classHelloWorld {
public static voidmain(String[] args) {System.out.println("Hello, World!");
}
}
The package statement and the class declaration are already there. Now
we are going to add the missing couple of lines.
8/14/2019 Creating and Running Your First Java Application
13/20
Place the cursor at the end of the current line, after {and press ENTER to
start a new line. (To start a new line, in fact, you don't even need to go to a
line end. Irrespective of the cursor position, pressing SHIFT+ENTER starts
a new line keeping the previous line intact.)
Using a live template for the main() method
The line
public static void main(String[] args) {}
may well be typed. However, we suggest that you use a different method.
Type:
psvm
and press TAB.
Here is the result:
What we have just used is a live template-based code generation facility.
Live templates used by the corresponding mechanism are code snippets
that can be quickly inserted into your code.
8/14/2019 Creating and Running Your First Java Application
14/20
A live template has an abbreviation, a string that identifies the template
(psvmin this example) and an expansion key, the keyboard key to be
pressed to insert the snippet into code (TAB in this case). For more
information, see Live Templatesin IntelliJ IDEA Help.
Using code completionNow, its time to add the remaining line of code
(System.out.println("Hello, World!");). Well do that using the IntelliJ
IDEA code completion. Type:
Sy
The code completion suggestion list is shown.
There is only one item in the list (System (java.lang)). Press ENTER to
select it.
Type:
.o
The suggestion list is shown again.
8/14/2019 Creating and Running Your First Java Application
15/20
Press ENTER to select out(there is only one item in the list again).
Type:
.printl
Note how the suggestion list changes as you type. The method we are
looking for is println(String x).
Select println(String x).
8/14/2019 Creating and Running Your First Java Application
16/20
Type:
"
Note that the second quotation mark is inserted automatically and the
cursor is placed between the quotation marks. Type:
Hello, World!
The code at this step is ready.
Using a live template for println()
As a side note, we could have inserted the call to println()by using a live
template. The abbreviation for the corresponding template is soutand the
expansion key is TAB. You can try using this template as an additional
exercise. (If you think that its enough for live templates, proceed to
Building the project).
Delete the line
System.out.println("Hello, World!");
Type:sout
and press TAB.
The line
System.out.println();
is added and the cursor is placed between (and ).
8/14/2019 Creating and Running Your First Java Application
17/20
Type "and then type Hello, World!
Building the project
The options for building a project or its parts are available in the Build
menu.
Many of these options are also available as context menu commands inthe Projecttool window, and in the editor for HelloWorld.java. (Check.)
Finally, there is an icon on the main toolbar that corresponds to the Make
Projectcommand ( ).
Now, lets build the project.
Building in this particular case is just compiling the Java source file into a
class file. So any of the options in the Buildmenu (Make Project, MakeModule HelloWorld, or Compile HelloWorld.java) can be used for this
purpose.
Lets try Build | Make Project. (The keyboard equivalent for this command
is CTRL+F9. Note that this shortcut is shown right in the menu as a useful
hint.)
Wait while IntelliJ IDEA is compiling the class. When this process is
complete, the corresponding information is shown on the Status bar.
Now, if you navigate to the module output folder (by default this is \out\production\; in our case,
and are both HelloWorld), youll find the folder structurefor the package com.example.helloworld there and the file
HelloWorld.classin the helloworldfolder.
8/14/2019 Creating and Running Your First Java Application
18/20
To find out more about building applications, see Build Process,
Compilation Types, Configuring Module Compiler Outputand Configuring
Project Compiler Outputin IntelliJ IDEA Help.
Running the application
Applications in IntelliJ IDEA are run according to what is called run/debug
configurations. Such configurations, generally, should be created prior torunning an application. (For more information, see Running, Debugging
and Testingin IntelliJ IDEA Help.)
In the case of the HelloWorldclass, there is no need to create a
run/debug configuration in advance. The class contains a main()method,
which marks it as a command-line executable. Such classes may be run
straight away, right from the editor. For this purpose, the Run
.main()command is provided in the context menu for the
class.
So, to run the class, right-click somewhere in the editing area and select
Run HelloWorld.main().
8/14/2019 Creating and Running Your First Java Application
19/20
As a result, the Runtool window opens at the bottom of the screen. This
tool window is responsible for displaying all the output from executed run
configurations. (For more information, see Run Tool Windowin IntelliJ
IDEA Help.)
The first line in the window shows the command line IntelliJ IDEA used to
run the class, including all options and arguments. The last line shows that
8/14/2019 Creating and Running Your First Java Application
20/20
the process has exited normally, and no infinite loops occurred. And,
finally, you see the program output Hello, World!between these two lines.
At this step, our exercises are complete. However, there are final remarks
worth making related to running applications in IntelliJ IDEA:
! The options for running applications can be found in the Runmenu.
Most of the command names in
this menu are self-explanatory. The Edit Configurationsoptionprovides access to a dedicated dialog for creating and editing run
configurations. Also note that keyboard shortcuts (shown right in the
menu) are available for most of the commands.
! On the main toolbar, there is an area containing controls related torunning applications. These include the run/debug configuration
selector and icons for running applications in various modes. The configuration selector, obviously, lets you
select a run/debug configuration that you want to be used. It also lets
you access the run/debug configuration settings (Edit
Configurations) and perform other tasks related to working withrun/debug configurations. (As a result of running the HelloWorldclass, a run/debug configuration HelloWorldwas created as a
temporary configuration. You can now save this run configuration(Save HelloWorld Configuration) to turn it into a permanent one.)
The options for running applications and for working with run/debugconfigurations, if appropriate, are also present as context menu commands
in the Projecttool window and the editor. (Check.)