Creating and running the first Java application (Part 1)
So, the JDK installation is completed, the package is created, the class is created, it's time to start writing the actual code... After creating the class, the HelloWorld.java file corresponding to it opens in the editor.
Notice the package statement at the beginning of the file, as well as the class declaration. When creating a class, IntelliJ IDEA uses a template file for the Java class. (IntelliJ IDEA provides a number of predefined templates for creating various types of files. For more information, see the File Templates section of the IntelliJ IDEA Help.) Also note the yellow light bulb. This lamp indicates that IntelliJ IDEA has suggestions for the current context. Click on the light bulb or ALT + ENTER to see a list of available actions.
At the moment, we are not going to perform the actions suggested by IntelliJ IDEA (such actions are called intention actions, see the Intention Actions section of the IntelliJ IDEA Help for more information about them.) Note, however, that this IntelliJ IDEA function sometimes can be very helpful. Finally, there are code folding markers next to the comments. Click one of them to collapse the corresponding block if you really don't want to see that part of the code at the moment. (You can also place the cursor in a block's code and then press CTRL+NumPad- or CTRL+NumPad+ to collapse or expand the block. For more information on code folding, see the Code Folding section of the IntelliJ IDEA Help . )
Using an active template for a method
Line:
and press TAB. As a result, we get:
In this case, we used the active template for generating object code. The active template has a string abbreviation that defines the template (PSVM = public static void main in this example) and a key to insert the snippet into the code (TAB in this case). For more information, see the Live Templates section of the IntelliJ IDEA Help.
In this case, there is only one option:
Type a dot and the letter " o ":
Press ENTER to select out .
Typing:
Choose
We print quotes:
The coding phase is now complete.
Many of these options are also available from the context menu in the Project window and in the editor for HelloWorld.java. There is also an icon on the toolbar that corresponds to the Make Project command . Now let's build the project. Building in this particular case is simply compiling a Java source file into a class file. So any of the options in the Build menu ( Make Project , Make Module 'HelloWorld' , or Compile 'HelloWorld.java') can be used for this purpose. Let's try to build a project. (The keyboard equivalent for this command is CTRL + F9. Note that this link appears directly in the menu as a helpful hint.) Wait for IntelliJ IDEA to finish compiling. When this process is completed, the relevant information is displayed in the status bar.
Now if you go to the output module folder (default is the folder
\out\production\
, in our case, and the folder
And
are called HelloWorld), you will see there the folder structure for the com.example.helloworld package and the HelloWorld.class file in the HelloWorld folder.
If you want to get a better understanding of building an application, see the IntelliJ IDEA Help:
Build Process ,
Compilation Types ,
Configuring Module Compiler Output and
Configuring Project Compiler Output sections .
As a result of executing the Run command, a window appears at the bottom of the screen. It windows is responsible for displaying all the output data specified in the command configuration. (For more information, see
Run Tool Window , in the IntelliJ IDEA Help.)
The first line in the window contains the IntelliJ IDEA command line used to run the class, including all options and arguments. The last line shows that the process exited normally, there were no infinite loops. And finally, you see the output of the Hello, World! between these two lines. At this point, our exercise is over. However, there are some final points to make regarding running IntelliJ IDEA applications:
Most of the command names in this menu are self-explanatory. The Edit Run Configuration option opens a dialog box for creating and editing run configurations. Also note that keyboard shortcuts (see menu on the right) are available for most commands.
Select Configuration allows you to select the Run/Debug configurations you wish to use. It also allows you to access Run/Debug configurations (Edit Configurations) and perform other tasks related to the operation of the Run/Debug functions. (As a result of running the HelloWorld class, the Run/Debug HelloWorld configuration has been saved as temporary. You can now save this run configuration (Save Configuration "HelloWorld") to make it permanent.)
![Building and Running Your First Java Application (Part 2) - 1](https://cdn.javarush.com/images/article/a1f19ec9-83b4-4925-9388-57784ff5846d/800.jpeg)
![Building and Running Your First Java Application (Part 2) - 2](https://cdn.javarush.com/images/article/64d98605-0ab6-4fec-b1e2-05cd6df44c80/512.jpeg)
![Creating and Running Your First Java Application (Part 2) - 3](https://cdn.javarush.com/images/article/7e920fb9-a660-4c39-9b9d-48837eaeca05/512.jpeg)
![Creating and Running Your First Java Application (Part 2) - 4](https://cdn.javarush.com/images/article/0880b85b-2d04-4ed0-b9fd-217b199c34f5/512.jpeg)
Writing code for the HelloWorld class
So, the moment has finally arrived. The final state code (as you probably know) will look like this:package com.example.helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The declaration of the package and class is already there, now let's add the missing couple of lines. Place the cursor at the end of the current line, after the { sign, and press ENTER to start a new line (Actually, you can make it easier: regardless of the cursor position, pressing SHIFT + ENTER starts a new line, keeping previous lines unchanged). ![Creating and Running Your First Java Application (Part 2) - 5](https://cdn.javarush.com/images/article/955b84ad-9645-46a4-a5b4-b40d2c36b0f6/512.jpeg)
Using an active template for a methodMain()
Line:
public static void main(String[] args) {}
quite easy to print. However, I would recommend another method. We print: psvm
![Building and Running Your First Java Application (Part 2) - 6](https://cdn.javarush.com/images/article/0dad8aba-6d45-4643-b714-17fae5c99367/512.jpeg)
![Building and Running Your First Java Application (Part 2) - 7](https://cdn.javarush.com/images/article/dc2ee536-4c6a-4f97-9a2d-68c63532e795/512.jpeg)
Using automatic code completion
Now it's time to add the remaining lines of code (System.out.println ("Hello, World!");
). We will do this with IntelliJ IDEA Code Completion. We print: Sy
Automatic code completion offers us options: ![Creating and Running Your First Java Application (Part 2) - 8](https://cdn.javarush.com/images/article/f1ef65a6-3c2f-4897-9f0e-e642e92a5f4e/512.jpeg)
System (java.lang)
. Press ENTER to select it. ![Creating and Running Your First Java Application (Part 2) - 9](https://cdn.javarush.com/images/article/bb4be4f8-c815-460e-b1b1-548716a27ea8/512.jpeg)
.о
The auto-complete feature again gives us options: ![Creating and Running Your First Java Application (Part 2) - 10](https://cdn.javarush.com/images/article/9b75750d-472b-49e5-a633-7e6fcf697313/512.jpeg)
![Creating and Running Your First Java Application (Part 2) - 11](https://cdn.javarush.com/images/article/a1fafdfd-086d-4890-bc02-11a78bd1047d/512.jpeg)
.printl
Notice how the list of options changes as you type. The method we are looking for is Println (String х)
. ![Building and Running Your First Java Application (Part 2) - 12](https://cdn.javarush.com/images/article/4c0eb11a-44ba-43d4-aeb9-508098409882/512.jpeg)
println(String x)
. The code takes the following form: ![Building and Running Your First Java Application (Part 2) - 13](https://cdn.javarush.com/images/article/1a76fc72-305d-4ec8-8899-efb86f25df37/512.jpeg)
"
As you can see, the second quotes appear automatically, and the cursor moves to the place where our text should be. Printing: Hello, World!
![Building and Running Your First Java Application (Part 2) - 14](https://cdn.javarush.com/images/article/1c5cc0c6-bbab-43ac-aca8-aa54f5d57e07/512.jpeg)
Using Active Template for Println()
By the way, we could make a callPrintln()
using the active template. The abbreviation for the corresponding template is Sout
. and the activation key is TAB. You can try this template as an additional exercise. (If you think you've had enough active templates, go ahead and create a project). Remove the line:
System.out.println("Hello, World!");
We print: sout
and press TAB. Line:
System.out.println();
is added automatically and the cursor is in brackets. We have to print: Hello, World!
Project construction
Options for building a project or part of it are available from the Build menu .![Building and Running Your First Java Application (Part 2) - 15](https://cdn.javarush.com/images/article/718d8cb8-0481-4b6a-bc49-f781996e0b98/512.jpeg)
![Creating and Running Your First Java Application (Part 2) - 16](https://cdn.javarush.com/images/article/e7423709-d168-494f-954e-5963c923a993/512.jpeg)
![Creating and Running Your First Java Application (Part 2) - 17](https://cdn.javarush.com/images/article/9c75e7b3-d2af-42b6-9c1b-a6c71bd830b1/512.jpeg)
Application launch.
An IntelliJ IDEA application runs according to what is called a run/debug configuration (Run/Debug). This configuration usually needs to be created prior to running the application. (See the section Running, Debugging and Testing in the IntelliJ IDEA Help for more information.) In the case of the HelloWorld class, there is no need to create a run and debug configuration beforehand. The class contains a main() method . Such classes can be launched immediately, directly from the editor. There is a command on the context menu for the class for this purposeRun ‘<ClаssName>.main()’
. So to run the class, right-click somewhere in the edit area and select
Run 'HellоWorld.main ()'
.
![Building and Running Your First Java Application (Part 2) - 18](https://cdn.javarush.com/images/article/05a53e86-7e15-4a79-927f-3ce707a38d91/512.jpeg)
![Building and Running Your First Java Application (Part 2) - 19](https://cdn.javarush.com/images/article/075f2970-6642-4c74-9832-7c8566a4fcd9/512.jpeg)
- Options for launching applications can be found in the main menu.
![Creating and Running Your First Java Application (Part 2) - 20](https://cdn.javarush.com/images/article/8d0bb2ee-15c9-4155-a6c2-c9469c3978f3/512.jpeg)
- The main toolbar has an area containing buttons related to launching applications. These include buttons for selecting the run and debug configuration (Run/Debug) and icons for launching applications in different modes.
![Building and Running Your First Java Application (Part 2) - 21](https://cdn.javarush.com/images/article/a0fa2857-52e6-4848-a7e9-038bba668787/original.jpeg)
![Creating and Running Your First Java Application (Part 2) - 22](https://cdn.javarush.com/images/article/804b1bc9-b8c1-4935-9610-6516a025fc6f/original.jpeg)
- Options for launching applications and for working with Run/Debug configurations, if necessary, are also present as context menu commands in the Project window .
GO TO FULL VERSION