Apr 12th, 2023
In the previous post, we showed you how to set up your coding environment for Java development using IntelliJ and Maven on Linux. In this post, we'll guide you through the process of creating a simple Java application using IntelliJ and Maven.
Step 1: Create a New Maven Project
Launch IntelliJ and select "Create New Project" from the welcome screen. Choose "Maven" as the project type and click "Next." Enter the group ID and artifact ID for your project and click "Next." Choose a project location and click "Finish." This will create a new Maven project with the default directory structure.
Step 2: Create a Java Class
In the project explorer, right-click on the "src/main/java" directory and select "New" > "Java Class." Enter the name of your class, for example, "HelloWorld," and click "OK." This will create a new Java class with a default template.
Step 3: Write Your Java Code
In the editor window, replace the default code with the following code:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }
This is a simple Java program that prints "Hello, world!" to the console.
Step 4: Build and Run Your Application
To build your application, open a terminal window and navigate to your project directory. Then, run the following command:
mvn package
This will compile your Java code and create a distributable JAR file in the "target" directory.
To run your application, use the following command:
java -jar target/myproject.jar
This will run your Java program and print "Hello, world!" to the console.
Congratulations! You've successfully created a simple Java application using IntelliJ and Maven on Linux. In the next post, we'll explore how to add external dependencies to your Java project using Maven.