JavaRush /Java Blog /Random EN /Building and Running Your First Java Application (Part 2)...
Ve4niY
Level 14

Building and Running Your First Java Application (Part 2)

Published in the Random EN group
Creating and running the first Java application (Part 1) Building and Running Your First Java Application (Part 2) - 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. Building and Running Your First Java Application (Part 2) - 2Notice 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. Creating and Running Your First Java Application (Part 2) - 3At 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 . ) Creating and Running Your First Java Application (Part 2) - 4

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

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) - 6and press TAB. As a result, we get: Building and Running Your First Java Application (Part 2) - 7In 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.

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) - 8In this case, there is only one option: System (java.lang). Press ENTER to select it. Creating and Running Your First Java Application (Part 2) - 9Type a dot and the letter " o ": The auto-complete feature again gives us options: Creating and Running Your First Java Application (Part 2) - 10Press ENTER to select out . Creating and Running Your First Java Application (Part 2) - 11Typing: .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) - 12Choose println(String x). The code takes the following form: Building and Running Your First Java Application (Part 2) - 13We print quotes: " 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) - 14The coding phase is now complete.

Using Active Template for Println()

By the way, we could make a call Println()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) - 15Many 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. Creating and Running Your First Java Application (Part 2) - 16Now 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. Creating and Running Your First Java Application (Part 2) - 17If 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 .

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 purpose Run ‘<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) - 18As 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.) Building and Running Your First Java Application (Part 2) - 19The 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:
  • Options for launching applications can be found in the main menu.
Creating and Running Your First Java Application (Part 2) - 20Most 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.
  • 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) - 21Select 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.) Creating and Running Your First Java Application (Part 2) - 22
  • Options for launching applications and for working with Run/Debug configurations, if necessary, are also present as context menu commands in the Project window .
Original article: Creating and running your first Java application Translated and voiced by: Ve4niY
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION