IntelliJ IDEA
 
Get IntelliJ IDEA
You are viewing the documentation for an earlier version of IntelliJ IDEA.

Run a Java application

Last modified: 11 February 2024

This tutorial explains how to quickly run a Java application. It also covers the setup required to run a Java application, such as creating a new project, configuring the JDK, and setting up run configurations with various options.

IntelliJ IDEA runs your code. After that, the Run tool window opens 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. 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 hangs for several seconds and then fails with OutOfMemoryError. Our program declares an infinite stream of integers and then tries to convert it into a list using the toList() method. Since the stream is infinite, the toList() method will never return, and the program will keep running indefinitely, consuming system resources.

Application failed with OutOfMemoryError

In this case, we can add a VM option that will create an .hprof file for us if our application fails with OutOfMemoryError. Later on, we will be able to analyze this file in detail using the built-in profiler.

This is useful if you do not know the reason for OutOfMemoryError and would like to investigate it further.

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