IntelliJ IDEA
 
Get IntelliJ IDEA

Tutorial: Run a Java application

Last modified: 11 November 2024

This tutorial explains how to run a Java application, use run/debug configurations, save program output to a file, and add custom VM options. It also covers the setup required to run a Java application, such as creating a new project and configuring the JDK.

IntelliJ IDEA runs your code and shows its output in the Run tool window at the bottom of the screen. The application has run successfully, that is why you will see the Process finished with exit code 0 message in the output.

Application has compiled

When you clicked Run, IntelliJ IDEA created a temporary run configuration named after the Main class. It defines the entry point and the parameters for running the application. So far, we haven't used any startup parameters, but we'll add them later.

The number of temporary configurations is limited to 5 by default, so the older ones are automatically deleted when new ones are added. That is why it is makes sense to save the temporary configurations that you want to keep.

Run configurations allow you to run the same application with different parameters. Now that you have two configurations, you can choose between them according to your needs. For example, if you do not need to save the console output every single time you run the application, you can run the Main configuration that does not have this setting.

Press AltShiftF10 or use the Run widget in the window header to switch between configurations:

Run widget switcher

Let's take a look at another scenario.

The application runs for several seconds and then fails with OutOfMemoryError. Our program creates an infinite stream of integers and then tries to collect it into a list using the toList() method. Since the stream is infinite, the toList() method will never return, and the program will keep consuming memory until it fails.

Application failed with OutOfMemoryError

If an application fails with OutOfMemoryError, we can add a VM option that will dump the memory to a .hprof file before crashing. Later on, we will be able to analyze this file in detail using the built-in profiler.

The Run tool window opens showing you that the OutOfMemoryError exception has been thrown. Since we have configured the corresponding VM option, the IDE has created an .hprof file in your project directory.

.hprof file has been created