How to Make a Program Run Again in Java

Create your start Java application

In this tutorial, you lot volition learn how to create, run, and package a simple Java application that prints Hello, World! to the system output. Along the style, you will get familiar with IntelliJ Idea features for boosting your productivity as a programmer: coding assistance and supplementary tools.

Spotter the screencast and follow the step-past-step instructions below:

Ready a project

Create a new Java projection

In IntelliJ Idea, a project helps you organize your source code, tests, libraries that you use, build instructions, and your personal settings in a unmarried unit.

  1. Launch IntelliJ IDEA.

    If the Welcome screen opens, click New Projection.

    Otherwise, from the principal menu, select .

  2. In the New Projection sorcerer, select New Project from the list on the left.

  3. Name the project (for case HelloWorld) and change the default location if necessary.

  4. We're not going to work with version command systems in this tutorial, so leave the Create Git repository option disabled.

  5. Make sure that the Java is selected in Language, and IntelliJ is selected in Build organisation.

    Creating a new Java project
  6. To develop Java applications in IntelliJ IDEA, y'all need the Java SDK (JDK).

    If the necessary JDK is already defined in IntelliJ IDEA, select information technology from the JDK listing.

    If the JDK is installed on your computer, merely not defined in the IDE, select and specify the path to the JDK home directory (for instance, /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk).

    Creating the new project and adding the JDK

    If you don't have the necessary JDK on your estimator, select . In the next dialog, specify the JDK vendor (for example, OpenJDK), version, change the installation path if required, and click Download.

    Downloading a JDK when creating a project
  7. Leave the Add sample lawmaking selection disabled as nosotros're going to do everything from scratch in this tutorial. Click Create.

After that, the IDE will create and load the new projection for you lot.

Create a package and a course

Packages are used for group together classes that belong to the same category or provide like functionality, for structuring and organizing large applications with hundreds of classes.

  1. In the Project tool window, right-click the src binder, select New (or press Alt+Insert), and then select Java Class.

  2. In the Name field, type com.instance.helloworld.HelloWorld and click OK.

    IntelliJ Thought creates the com.example.helloworld package and the HelloWorld class.

Together with the file, IntelliJ Thought has automatically generated some contents for your class. In this case, the IDE has inserted the package argument and the class declaration.

This is done by ways of file templates. Depending on the type of the file that you create, the IDE inserts initial lawmaking and formatting that is expected to be in all files of that blazon. For more than data on how to use and configure templates, refer to File templates.

Write the code

Add the main() method using live templates

  1. Identify the caret at the form declaration string afterward the opening bracket { and press Shift+Enter.

    In dissimilarity to Enter, Shift+Enter starts a new line without breaking the current one.

  2. Type main and select the template that inserts the main() method declaration.

    As you type, IntelliJ IDEA suggests various constructs that can be used in the current context. Yous can come across the list of available live templates using Ctrl+J.

Alive templates are code snippets that you tin can insert into your code. chief is one of such snippets. Normally, live templates comprise blocks of code that you use well-nigh often. Using them can save yous some time as y'all don't have to type the same code over and over again.

For more than information on where to find predefined live templates and how to create your own, refer to Live templates.

Telephone call the println() method using lawmaking completion

After the master() method announcement, IntelliJ Idea automatically places the caret at the adjacent line. Let's telephone call a method that prints some text to the standard organisation output.

  1. Blazon Sy and select the Organization form from the listing of code completion suggestions (it's from the standard java.lang package).

    Press Ctrl+. to insert the selection with a trailing comma.

  2. Type o, select out, and press Ctrl+. again.

  3. Type p, select the println(String x) method, and press Enter.

    IntelliJ IDEA shows you the types of parameters that tin be used in the current context. This information is for your reference.

  4. Blazon ". The second quotation marker is inserted automatically, and the caret is placed between the quotation marks. Type Hello, World!

Basic code completion analyses the context around the current caret position and provides suggestions equally you type. You can open up the completion list manually by pressing Ctrl+Space.

For information on different completion modes, refer to Code completion.

Telephone call the println() method using a live template

You tin call the println() method much quicker using the sout alive template.

After the primary() method declaration, IntelliJ Thought automatically places the caret at the next line. Permit's call a method that prints some text to the standard arrangement output.

  1. Type sout and press Enter.

  2. Type ". The second quotation mark is inserted automatically, and the caret is placed between the quotation marks. Type Hello, World!.

Build and run the application

Valid Java classes can be compiled into bytecode. You tin can compile and run classes with the master() method right from the editor using the green arrow icon the Run the Remove button in the gutter.

  1. Click the Run the Remove button in the gutter and select Run 'HelloWorld.main()' in the popup. The IDE starts compiling your lawmaking.

  2. When the compilation is complete, the Run tool window opens at the lesser of the screen.

    The starting time line shows the command that IntelliJ IDEA used to run the compiled class. The second line shows the program output: Hello, World!. And the final line shows the go out code 0, which indicates that it exited successfully.

    If your code is not right, and the IDE can't compile it, the Run tool window will display the corresponding exit lawmaking.

When you click Run, IntelliJ Thought creates a special run configuration that performs a series of actions. Commencement, information technology builds your application. On this phase, javac compiles your source lawmaking into JVM bytecode.

In one case javac finishes compilation, it places the compiled bytecode to the out directory, which is highlighted with yellow in the Project tool window.

Later on that, the JVM runs the bytecode.

IntelliJ IDEA automatically analyzes the file that is currently opened in the editor and searches for dissimilar types of problems: from syntax errors to typos. The Inspections widget at the top-right corner of the editor allows you to rapidly see all the detected problems and look at each problem in detail. For more information, refer to Current file.

Parcel the application in a JAR

When the lawmaking is gear up, you can packet your application in a Java archive (JAR) and then that you tin share information technology with other developers. A built Java archive is called an artifact.

Create an artifact configuration for the JAR

  1. From the main bill of fare, select File | Project Structure (Ctrl+Alt+Shift+S) and click Artifacts.

  2. Click the Add button, point to JAR and select From modules with dependencies.

  3. To the right of the Main Form field, click the Browse button and select HelloWorld (com.example.helloworld) in the dialog that opens.

    IntelliJ Idea creates the artifact configuration and shows its settings in the correct-hand part of the Project Construction dialog.

  4. Utilise the changes and close the dialog.

Build the JAR antiquity

  1. From the main carte, select Build | Build Artifacts.

  2. Signal to HelloWorld:jar and select Build.

    Building an artifact

    If you now look at the out/artifacts folder, you'll observe your JAR there.

    The JAR artifact is built

Run the packaged application

To make sure that the JAR artifact is created correctly, y'all can run information technology.

Create a run configuration for the packaged application

To run a Java application packaged in a JAR, IntelliJ IDEA allows you to create a dedicated run configuration.

  1. Press Ctrl+Shift+A, discover and run the Edit Configurations action.

  2. In the Run/Debug Configurations dialog, click the Add button and select JAR Awarding.

  3. Proper name the new configuration: HelloWorldJar.

  4. In the Path to JAR field, click the Browse button and specify the path to the JAR file on your reckoner.

  5. Scroll downwardly the dialog and under Before launch, click the Add button, select .

    Doing this means that the HelloWorld.jar is built automatically every time you lot execute this run configuration.

Run configurations allow you to ascertain how you want to run your application, with which arguments and options. Yous tin can have multiple run configurations for the same awarding, each with its own settings.

Execute the run configuration

  • On the toolbar, select the HelloWorldJar configuration and click the Run button to the correct of the run configuration selector. Alternatively, printing Shift+F10 if you prefer shortcuts.

    As before, the Run tool window opens and shows yous the application output.

The process has exited successfully, which ways that the application is packaged correctly.

Last modified: 17 March 2022

elliottmompok.blogspot.com

Source: https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html

0 Response to "How to Make a Program Run Again in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel