Learn how to code Hello World in Java using IntelliJ and Maven.
1. Introduction
This blog will teach you how to create a Hello World application using a Maven archetype in Java using IntelliJ IDE.
2. Requirements
You must have the following installed on your local machine.
- OpenJDK 11
- Maven
- IntelliJ community edition
3. Creating a new Java project from a Maven archetype
3.1 Creating the project
Open IntelliJ, and you should be greeted with IntelliJ's Welcome screen.
If this is your first time running IntelliJ, then the project panel should be empty.
Click New Project and select Maven. In the right panel, click "Create from archetype" and find and select "maven-archetype-quickstart."
*archetype is a project template that automatically includes dependencies depending on the purpose of the project. The particular template that we have selected includes JUnit dependency.
*You can search for dependency signature from https://mvnrepository.com.
Click Next.
In the next screen, you must enter the project's artifact coordinates:
In this case, our project name is hello-intellij-training, typically you should use the same for the artifact id.
GroupId: must be something unique to your organization, here we are using this blog's domain name. Doesn't really need to be a domain name, nor must it exists as an active URL. But this is the standard.
Click Next, and a project summary should be presented.
Click Finish. Give it some time to download the archetype and initialize your project.
This is how our project should look like. Notice that it's using Java 1.7 (1.7) by default. We should replace it with 11.
3.2 Setting the correct Java version in IDE
Before we could print our hello world message, we should first configure the Java version for the project.
Select hello-intellij-training in the Project's panel and, click File in the top menu, select Project Structure.
There are two things to check here related to Java.
3.2.1 Under Project Structure / Project Settings / Project, left panel Project SDK select 11. If the dropdown is empty, click Edit and finds the directory where you installed OpenJdk 11.
3.2.2 This time, open Project Structure / Project Settings / Module and select hello-intellij-training. In this case, we only have one module. But if you are working on a multi-project, you should have several entries here. In the rightmost panel, under Sources / Language level, select 11. This is a common source of compilation issues. Always make sure you are compiling and running on the same version of Java.
Click Apply.
Now we are ready to print our very first hello world message.
4. Printing our Hello World message
By default, the archetype should already make available an App class that prints the hello world message. Let's change it to "Hello IntelliJ!".
5. Running the application
Let's build the application first.
In the right panel, toggle Maven, expand hello-intellij-training / Lifecycle. Right-click on install and select Run Maven Build.
This should build our project. Whenever you have revisions on your project, select install. If you remove some files, run "clean" first, followed by install.
Another approach to building the module is by using IntelliJ's builder.
You can right-click on the module and select Build Module hello-intellij-training.
You can also do the same using the main navigation. Build project or module.
By default IntelliJ uses ant, to delegate the building process using Maven we need to do the following.
In the main menu, select File / Settings.
Expand Build, Execution, Deployment / Build Tools / Maven and select Runner. In the right panel, click Delegate IDE build/run actions to Maven. Click Ok.
Now try building the module again and in the Build log, you should see maven logs.
There are several ways to run the maven application.
5.1 In your App class, there should be a green arrow in the gutter, click it, and it should show a popup where you can either select Run or Debug.
You could also put the cursor in the class and press the Run shortcut Ctrl + Shift + F10.
The Run panel should show at the bottom.
5.2 If you run 5.1 first, you should see App in the top right run configuration. Otherwise, it should be empty.
From here, you can click the dropdown (whether you have App or not).
If you already run the App class, it should look like this:
But in some cases, you might want to create a Run Configuration from a template. For example, Spring Boot.
Click the plus icon in the Run/Debug Configurations panel and select Add New Configuration.
You should be presented with a list of IntelliJ's supported Run/Debug configuration templates.
If you select Spring Boot, this is how it should look like.
Top comments (0)