Write Hello World in Java Using Intellij and Maven
Tags: intellij, java, maven, helloworld
Published: 2023-01-02 03:14:41

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t30fnfj2bcnz4tllsk8q.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ei972vvl01lvw0kue9tk.png)

Click Next.

In the next screen, you must enter the project's artifact coordinates:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qimzsa0cjuael7ymnn1.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yujgnrtxosxaehu8d80x.png)

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 (<maven.compiler.source>1.7</maven.compiler.source>) by default. We should replace it with 11.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1op7shd2hyrbdxtskhis.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12pdw992xeywnwsgipq1.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvtkxo7zyvm1fstwwaa5.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbwkcdoi96duflaqub8t.png)

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!".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ate3zprvrp22b9zdylsw.png)

# 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxcssi3ixpcvj0urh53s.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yz39hqsa9bzf1dxh9t1o.png)

You can also do the same using the main navigation. Build project or module.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aaq9elzgbhv3q642et2d.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nl1dqvc12rpsftgik725.png)

Expand Build, Execution, Deployment / Build Tools / Maven and select Runner. In the right panel, click Delegate IDE build/run actions to Maven. Click Ok.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmcv4hojnw9fj50j229f.png)

Now try building the module again and in the Build log, you should see maven logs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpfqhnvwzqdgr80g23tg.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qehiw8fgltl0qirlqjm.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvu9z22cdrmp0wc4jjny.png)

5.2 If you run 5.1 first, you should see App in the top right run configuration. Otherwise, it should be empty.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pwe78pgmsqmwf6nq6ci.png)

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pigoljuiavlq2starsvc.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v92e8an3yks8jawlej61.png)