IntelliJ IDEA 2021.2 Help

Run applications

You can run applications right from IntelliJ IDEA if you have an SDK set up for your project/module.

Quick way

If you are not going to pass any parameters to your program, and your program does not require any specific actions to be performed before start, you can run it right from the editor.

  • Click Icons actions execute in the gutter near the class declaration and select Run.

    Run quick 1

Customizable way

If you are going to pass parameters to your program, add VM options (for example, to allow remote debugging), or otherwise customize the startup of your program, use a run/debug configuration.

  1. Click Icons actions execute in the gutter near the class declaration and select Modify Run Configuration.

    Run customizable 1
  2. Modify the run/debug configuration as needed. For example, if you need to run your program with arguments, add the arguments to the Program arguments field.

    To access additional parameters, click Modify options and select the required option from the menu.

    Run customizable 2

    Click OK to apply the changes.

  3. Click The Run button or press Shift+F10.

When the application starts, you can view its output and interact with it in the Run tool window. Every run/debug configuration creates a separate tab when you run it.

A tab in the Run tool window

To learn more about tool windows and how to manage them, see the Tool windows topic.

Re-run applications

  • On the toolbar of the Run tool window, click the Rerun button or press Shift+F10

    Rerun button

Stop and pause applications

When you stop a program, its process is interrupted and exits immediately. When you pause a program, it continues running in the background, but its output is suspended.

Stop a program

  • In the Run tool window, click the Stop button on the toolbar. Alternatively, press Ctrl+F2 and select the process to stop.

Pause a program

  • Right-click in the Run tool window and select Pause Output from the context menu. Use the same toggle to resume the program.

    Pause output

Deal with errors

If there are errors in your program, here is how you can address them.

  • If an exception is thrown:

    • If the meaning of the exception is unclear, you can navigate to the exception class right from the stack trace and read the documentation for the exception. This may help you understand its cause. In some cases, this is enough to sort out the problem.

    • Attach the debugger and examine the program state that led to the failure. See Tutorial: Debug your first Java application for a quick introduction to IntelliJ IDEA debugger.

    • Run static analysis to see where incorrect values might be coming from.

  • If a logic error is present, the debugger may save you a lot of time finding and fixing the cause. See Tutorial: Debug your first Java application for a quick introduction to IntelliJ IDEA debugger.

  • If the problem is related to application performance, IntelliJ IDEA profiler will help you analyze the problem and assess how efficient your fix is.

Shebang scripts

Starting with version 11, Java provides a way to run self-contained scripts without the need to compile them (https://openjdk.java.net/jeps/330). Furthermore, on Linux and macOS, you can make a Java script executable by specifying the JDK in the first line of the script.

This is called the shebang mechanism. Shebang support in Java comes in handy when you need to write an executable script, but cannot use a scripting language like bash or Python.

Write a script

  1. Create a file without the .java extension.

  2. Start the first line with #! followed by the path to the JDK that will be used to run the script. Use source to specify the language level of the script. Language level 11+ is required.

    Example:

    #!/usr/lib/jvm/openjdk-14.0.1/bin/java --source 11

  3. Write the body of the script. The script can contain multiple classes and use imports from the standard library. The entry point has to be defined as public static void main(String[] args) in the first declared class.

    Below is an example of a valid shebang script:

    #!/usr/lib/jvm/openjdk-14.0.1/bin/java --source 11 import java.util.Locale; class Hello { public static void main(String[] args) { String lang = Locale.getDefault().getLanguage(); System.out.println(Greetings.getGreeting(lang)); } } class Greetings { static String getGreeting(String lang) { switch (lang) { case "fr": return "Bonjour"; case "es": return "Hola"; case "zh": return "Nǐn hǎo"; case "de": return "Guten Tag"; case "pl": return "Dzień dobry"; case "el": return "Yassas"; case "sv": return "God dag"; default: return "Hi"; } } }
  4. Make sure that the script file is executable using the chmod +x command.

Run a script

  • Click the Run icon in the gutter or press Ctrl+Shift+F10.

    Shebang run
  • Create a run/debug configuration. This can be useful if you want to pass parameters to your script, debug it, or run it as part of a compound workflow.

Debug a script

  1. Create a run/debug configuration for the script and add a VM option to load the debug agent.

  2. Attach to the process using the Remote JVM debug run/debug configuration.

Show running processes

You can view the list of all active run or debug sessions and navigate between them.

  • From the main menu, select Run | Show Running List. In the top-right corner of the editor, IntelliJ IDEA shows a list with all active applications.

    A list of running applications
Last modified: 02 August 2022