Create your first Java application
In this tutorial, you will learn how to create, run, and package a simple Java application that prints Hello, World! to the system output. Along the way, you will get familiar with IntelliJ IDEA features for boosting your productivity as a developer: coding assistance and supplementary tools.
Prepare a project
Create a new Java project
In IntelliJ IDEA, a project helps you organize your source code, tests, libraries that you use, build instructions, and your personal settings in a single unit.
Launch IntelliJ IDEA.
If the Welcome screen opens, click New Project .
Otherwise, from the main menu, select File | New Project .
In the New Project wizard, select New Project from the list on the left.
Name the project (for example HelloWorld ) and change the default location if necessary.
We’re not going to work with version control systems in this tutorial, so leave the Create Git repository option disabled.
Make sure that Java is selected in Language , and IntelliJ is selected in Build system .
To develop Java applications in IntelliJ IDEA, you need the Java SDK ( JDK ).
If the necessary JDK is already defined in IntelliJ IDEA, select it from the JDK list.
If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory (for example, /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk ).
If you don’t have the necessary JDK on your computer, select Download JDK . In the next dialog, specify the JDK vendor (for example, OpenJDK), version, change the installation path if required, and click Download .
Leave the Add sample code option disabled as we’re going to do everything from scratch in this tutorial. Click Create .
After that, the IDE will create and load the new project for you.
Create a package and a class
Packages are used for grouping together classes that belong to the same category or provide similar functionality, for structuring and organizing large applications with hundreds of classes.
In the Project tool window, right-click the src folder, select New (or press Alt+Insert ), and then select Java Class .
In the Name field, type com.example.helloworld.HelloWorld and click OK .
IntelliJ IDEA creates the com.example.helloworld package and the HelloWorld class.
Together with the file, IntelliJ IDEA has automatically generated some contents for your class. In this case, the IDE has inserted the package statement and the class declaration.
This is done by means of file templates. Depending on the type of the file that you create, the IDE inserts initial code and formatting that is expected to be in all files of that type. For more information on how to use and configure templates, refer to File templates.
The Project tool window Alt+1 displays the structure of your application and helps you browse the project.
In Java, there’s a naming convention that you should follow when you name packages and classes.
Write the code
Add the main() method using live templates
Place the caret at the class declaration string after the opening bracket < and press Shift+Enter .
In contrast to Enter , Shift+Enter starts a new line without breaking the current one.
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. You can see the list of available live templates using Ctrl+J .
Live templates are code snippets that you can insert into your code. main is one of such snippets. Usually, live templates contain blocks of code that you use most often. Using them can save you some time as you don’t have to type the same code over and over again.
For more information on where to find predefined live templates and how to create your own, refer to Live templates.
Call the println() method using code completion
After the main() method declaration, IntelliJ IDEA automatically places the caret at the next line. Let’s call a method that prints some text to the standard system output.
Type Sy and select the System class from the list of code completion suggestions (it’s from the standard java.lang package).
Press Ctrl+. to insert the selection with a trailing period.
Type o , select out , and press Ctrl+. again.
Type p , select the println(String x) method, and press Enter .
IntelliJ IDEA shows you the types of parameters that can be used in the current context. This information is for your reference.
Type " . The second quotation mark 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 as you type. You can open the completion list manually by pressing Ctrl+Space .
For information on different completion modes, refer to Code completion.
Call the println() method using a live template
You can call the println() method much quicker using the sout live template.
After the main() method declaration, IntelliJ IDEA automatically places the caret at the next line. Let’s call a method that prints some text to the standard system output.
Type sout and press Enter .
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 can compile and run classes with the main() method right from the editor using the green arrow icon in the gutter.
Click in the gutter and select Run ‘HelloWorld.main()’ in the popup. The IDE starts compiling your code.
When the compilation is complete, the Run tool window opens at the bottom of the screen.
The first line shows the command that IntelliJ IDEA used to run the compiled class. The second line shows the program output: Hello, World! . And the last line shows the exit code 0 , which indicates that it exited successfully.
If your code is not correct, and the IDE can’t compile it, the Run tool window will display the corresponding exit code.
When you click Run , IntelliJ IDEA creates a special run configuration that performs a series of actions. First, it builds your application. On this stage, javac compiles your source code into JVM bytecode.
Once javac finishes compilation, it places the compiled bytecode to the out directory, which is highlighted with yellow in the Project tool window.
After that, the JVM runs the bytecode.
Automatically created run configurations are temporary, but you can modify and save them.
If you want to reopen the Run tool window, press Alt+4 .
IntelliJ IDEA automatically analyzes the file that is currently opened in the editor and searches for different types of problems: from syntax errors to typos. The Inspections widget at the top-right corner of the editor allows you to quickly see all the detected problems and look at each problem in detail. For more information, refer to Current file.
Package the application in a JAR
When the code is ready, you can package your application in a Java archive (JAR) so that you can share it with other developers. A built Java archive is called an artifact .
Create an artifact configuration for the JAR
From the main menu, select File | Project Structure ( Ctrl+Alt+Shift+S ) and click Artifacts .
Click , point to JAR and select From modules with dependencies .
To the right of the Main Class field, click and select HelloWorld (com.example.helloworld) in the dialog that opens.
IntelliJ IDEA creates the artifact configuration and shows its settings in the right-hand part of the Project Structure dialog.
Apply the changes and close the dialog.
Build the JAR artifact
From the main menu, select Build | Build Artifacts .
Point to HelloWorld:jar and select Build .

If you now look at the out/artifacts folder, you’ll find your JAR there.
Run the packaged application
To make sure that the JAR artifact is created correctly, you can run it.
Use Find Action Ctrl+Shift+A to search for actions and settings across the entire IDE.
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.
Press Ctrl+Shift+A , find and run the Edit Configurations action.
In the Run/Debug Configurations dialog, click and select JAR Application .
Name the new configuration: HelloWorldJar .
In the Path to JAR field, click and specify the path to the JAR file on your computer.
Scroll down the dialog and under Before launch , click , select Build Artifacts | HelloWorld:jar .
Doing this means that the HelloWorld.jar is built automatically every time you execute this run configuration.
Run configurations allow you to define how you want to run your application, with which arguments and options. You can have multiple run configurations for the same application, each with its own settings.
Execute the run configuration
On the toolbar, select the HelloWorldJar configuration and click to the right of the run configuration selector. Alternatively, press Shift+F10 if you prefer shortcuts.
As before, the Run tool window opens and shows you the application output.
The process has exited successfully, which means that the application is packaged correctly.
Packaging the Application
We can package the application into a Java ARchive file (JAR).
When the code is ready, we can package our application in a JAR. JAR files are often used to deploy an application to the production server. Once a JAR file has been built, it is called an artifact. Let's take look at how to create artifacts in IntelliJ IDEA.
Creating an Artifact
1) Press Cmd+; on macOS, or Shift+Control+Alt+S on Windows to bring up the Project Structure dialog.
2) Select Artifacts from the left-hand menu and then press the + icon. Select JAR and then From modules with dependencies.
You don't need to change anything for the Module, however you do need to say which class in your project has the main method. Click the browse button to navigate to your main method. IntelliJ IDEA will show you a list of classes in your project, you only have one so select that.
3) Press OK to select your class. All the other defaults are fine for this tutorial, press OK. Now you can see your new JAR file defined in the Project Structure dialog.
4) If it matches the above screenshot, press OK. You have now defined how to build the JAR file, but you haven't actually built it yet. You need to build it with your build artifacts.
5) Go to Build > Build Artifacts. You will only have one to choose from which is the one that we just defined.
6) Press Enter to build the artifact. Your status bar at the bottom-left of the screen will show you when this has completed.
IntelliJ IDEA puts the artifact in a new folder in your out folder called HelloWorld_jar .
To make sure that this JAR file was created correctly you will want to run it. We'll do this in the next step of this tutorial by using a run configuration.
How to build an artifact from simple java application project in IntelliJ?
Continuing from the previous article about how to start a simple java application project in IntelliJ, I would like to talk about how to build an artifact(Java Archive File) from the project in IntelliJ.
Option1. Compile a single file or class.
Before we actually begin to compile the whole files or classes, I would like to show you how to compile a single file or class and what comes out of it as a result.
Step1. Press the button Recompile ‘ClassName.java’.
You can find the button in right-click menu or Build menu at the top.
What the Recompile does is to compile the java file into class file into the output directory.
as you can see the compiled Main.class file resides in out/production/SimpleApplication folder. That is because IntelliJ basically follows the output directory rules as followings:
- Sources: <ProjectFolder>/out/production/<ModuleName>
- Tests: <ProjectFolder>/out/test/<ModuleName>
Here what <ModuleName> indicate is the SimpleApplication module itself.
The SimpleApplication module has been created alongside the project creation. You can see the list of modules in Project Structure window ( Shortcut : Command + ; ) as in the picture below.
Step2. Execute the compiled class file for test.
You can actually execute the class file using ‘java’ command.
of course, to use java command, you need it installed somewhere else like this. using “which java” lets you know where it is installed or symbolically linked.
Option2. Build a module or project.
The reason why I gave the option1 which is unrealistic in real world is because I am sure that every single problem that looks bigger than what it is is because we mostly tend to ignore the smallest unit of problem like option1.
In reality, we are supposed to be building entire project so that we can build an artifact from the hundreds, thousands of java files.
To test building entire classes in the target directory, I have added another class name Util and referenced it in the Main class’s method like this.
Как создать исполняемый jar в Intellij IDEA / how to create jar in IDEA

-
Нажмите кнопку ‘+’ в диалоговом окне Project Structure и выберите соответствующий пункт (Рисунок 1):
Рисунок 1 – Project Structure -> Artifacts -> Jar -> From modules with dependencies

Далее IntelliJ IDEA показывает диалог, позволяющий настроить новый артефакт (Рисунок 2):
Здесь нужно выбрать главный класс вашего проекта и нажать ОK
Рисунок 2 – Диалог создания нового артефакта JAR
После нажатия клавиши ОК, вы можете создать файл Jar с помощью пункта меню Build -> Build Artifact
По умолчанию все библиотеки будут извлечены в целевой Jar. Сам исполняемый файл будет сгенерирован в директории out -> artifacts вашего проекта (Рисунок 3): 
Рисунок 3 – Месторасположение сгенерированного JAR файла
Все, теперь исполняемый файл можно запускать.